Redis缓存工具类(夏顺辉)

来源:互联网 发布:怎样才能加入淘宝商城 编辑:程序博客网 时间:2024/06/07 15:07

这里写图片描述

1、首先前提是集成了mvn-redis模块

RedisAspect.java代码如下:

/* * 文件名:ddd.java * 版权:©Copyright by www.sowell-tech.cn * 描述: * 修改人:Administrator * 修改时间:2017年4月17日 * 修改内容: */package com.sowell.service.impl.catchService;import java.util.Arrays;import java.util.List;import org.apache.log4j.Logger;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.sowell.common.util.StringUtil;//把这个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面@Aspect@Componentpublic class RedisAspect {    @Autowired    RedisService  redisService;    /**     * 日志     */    private final Logger logger = Logger.getLogger(getClass());    //坏绕通知:需要携带ProceedingJoinPoint类型的参数    //环绕通知类似于动态代理的全过程:ProceedingJoinPoint类型的参数可以决定是否执行目标方法    //且环绕通知必须有返回值,返回值即目标方法的返回值。    @Around("execution(* com.sowell.controller.*Controller.*(..))")    public Object aroundMethod(ProceedingJoinPoint pjd) {        Object result = null;        String methodName = pjd.getSignature().getName();        Object args = Arrays.asList(pjd.getArgs());        //执行目标方法        try {            logger.info("request channels begin, param{pageNum:" + methodName + ", pageSize:" + args);            //前置通知            System.out.println("Arround:The method "+methodName +" begins with "+ args);              String key = methodName+"_"+args;            String jsonStr = redisService.getJsonString(key);            // 如果数据库里配置的redis服务端中已经存在了这个key对应的数据,那么就将这个数据返回            if (!StringUtil.isEmpty(jsonStr))            {               return jsonStr;            }            // 这条语句一直是切面前的逻辑,此方法的返回结果是调用此controller的返回值            result = pjd.proceed();            // 下面是切面后的逻辑。如果没有存储这个数据,那么就添加到redis中            redisService.setString(key,result.toString());            //后置通知            System.out.println("Arround:The method "+ methodName+" ends");        } catch (Throwable e) {            e.printStackTrace();            //异常通知            System.out.println("Arround:The method "+ methodName+"occurs exception:"+e);            //throw new RuntimeException(e);            //不抛出异常的话,异常就被上面抓住,执行下去,返回result,result值为null,转换为int        }        //返回通知        System.out.println("Arround:The method "+ methodName+" ends with the Result "+ result);        return result;    }}

RedisService.java代码如下:

package com.sowell.service.impl.catchService;import org.springframework.stereotype.Service;import com.sowell.common.util.FastJsonUtils;import com.sowell.redis.client.RedisClient;import com.sowell.redis.route.RedisRoute;/** * 此处为类说明 * @author xiashunhui * @version v1.0 * @since v1.0 2017年4月17日 * @see 下午3:53:35 */@Servicepublic class RedisService {    public String setObject(String key ,Object obj)    {          // 获取redis客户端         RedisClient rClient = null;         String jsonstr =null;         try         {             rClient = RedisRoute.getRedisClient("");             jsonstr  =FastJsonUtils.toJSONString(obj);             if (null != rClient)             {               rClient.setStr(key, jsonstr);             }         }         catch (Exception e)         {            e.printStackTrace();            return jsonstr;         }         return jsonstr;    }    public String setString(String key ,String objstr)    {          // 获取redis客户端         RedisClient rClient = null;         try         {             rClient = RedisRoute.getRedisClient("");             if (null != rClient)             {               rClient.setStr(key, objstr);             }         }         catch (Exception e)         {            e.printStackTrace();            return objstr;         }         return objstr;    }   public String  getJsonString(String key)    {       String result =null;         // 获取redis客户端        RedisClient rClient = null;        try        {               //如果参数为"",那么获取到的client也会为null,如果            rClient = RedisRoute.getRedisClient("");            if (null != rClient)            {                result = rClient.getStr(key);            }        }        catch (Exception e)        {           e.printStackTrace();        }        return result;    }   public String delByKey(String key)   {         // 获取redis客户端        RedisClient rClient = null;        String jsonstr =null;        try        {            rClient = RedisRoute.getRedisClient("");            if (null != rClient)            {              rClient.delByDataType(key);            }        }        catch (Exception e)        {           e.printStackTrace();           return jsonstr;        }        return jsonstr;   }}
0 0