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
- Redis缓存工具类(夏顺辉)
- 单服务缓存redis工具类
- Redis缓存数据库操作类(Java)
- php+redis缓存类
- PHP + redis缓存类
- Redis工具类获取Redis
- spring缓存(二)--redis缓存
- 工具类 - 缓存
- ehcache 缓存工具类
- android缓存工具类
- 图片缓存工具类
- 本地缓存工具类
- 缓存工具类
- Android缓存工具类
- 缓存工具类封装
- android 缓存工具类
- 图片缓存工具类
- 图片缓存工具类
- 基于 Django1.10 文档的深入学习(7)—— Configuring applications
- android 自定义View--圆环颜色选择器
- cxf调用webService
- Spring中IOC与AOP的理解
- Linux运维-Linux系统日常管理
- Redis缓存工具类(夏顺辉)
- std::mutex std::unique_lock std::lock_guard std::recursive_mutex的理解
- CCBPM在舆情系统中的应用
- spark mllib源码分析之随机森林(Random Forest)(二)
- InputStream的mark和reset
- BZOJ 2733 [HNOI2012] 永无乡 [线段树+并查集做法]
- Linux中对文件描述符的操作(FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- java.util.regex.PatternSyntaxException: Unclosed counted closure near index 14
- 一个只有99行代码的JS流程框架(二)