设为首页 收藏本站
查看: 890|回复: 0

[经验分享] redis实现 spring-redis-data初学习

[复制链接]

尚未签到

发表于 2018-11-6 08:35:30 | 显示全部楼层 |阅读模式
  今天看了一些redis的客户端实现、主要分为spring-redis-data 、jredis
  今天先记录下spring-redis-data的学习心得;
  spring-redis-data 中我目前主要用了它的存、取、清除。
  先看配置吧redis-manager-config.properties :
  [html] view plaincopy

  •   redis.host=192.168.1.20//redis的服务器地址
  •   redis.port=6400//redis的服务端口
  •   redis.pass=1234xxxxx//密码
  •   redis.default.db=0//链接数据库
  •   redis.timeout=100000//客户端超时时间单位是毫秒
  •   redis.maxActive=300// 最大连接数
  •   redis.maxIdle=100//最大空闲数
  [html] view plaincopy

  •   redis.maxWait=1000//最大建立连接等待时间
  •   redis.testOnBorrow=true//指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
  spring 中配置
  [html] view plaincopy

  •   
  •   
  •   
  •   
  •   
  •   classpath:config/redis-manager-config.properties
  •   
  •   
  •   

  •   
  •   
  •   
  •   
  •   
  •   
  •   

  •   
  •   
  •   
  •   
  •   
  •   
  •   
  •   
  •   
  •   

  •   
  •   
  •   
  [html] view plaincopy


  [html] view plaincopy

  •   
  [html] view plaincopy

  •   
  •   
  •   
  java代码:
  [java] view plaincopy

  •   public class RedisBase {

  •   private StringRedisTemplate template;

  •   /**
  •   * @return the template
  •   */
  •   public StringRedisTemplate getTemplate() {
  •   return template;
  •   }

  •   /**
  •   * @param template the template to set
  •   */
  •   public void setTemplate(StringRedisTemplate template) {
  •   this.template = template;
  •   }

  •   }
  继续:
  下面就是具体redis的值的写入、读出、清除缓存喽!
  第一:写入
  [java] view plaincopy

  •   public class StudentCountDO {

  •   private Long id;

  •   private String studentId;

  •   private Long commentHeadCount;

  •   private Long docAttitudeScores;

  •   private Long guideServiceScores;

  •   private Long treatEffectCount;

  •   private Long treatEffectScores;

  •   private String gmtModified;

  •   private String gmtCreated;

  •   private Long waitingTimeScores;

  •   }
  [java] view plaincopy

  •   StringRedisTemplate template = getTemplate();//获得上面注入的template
  •   // save as hash 一般key都要加一个前缀,方便清除所有的这类key
  •   BoundHashOperations ops = template.boundHashOps("student:"+studentCount.getStudentId());

  •   Map data = new HashMap();
  •   data.put("studentId", CommentUtils.convertNull(studentCount.getStudentId()));
  •   data.put("commentHeadCount", CommentUtils.convertLongToString(studentCount.getCommentHeadCount()));
  •   data.put("docAttitudeScores", CommentUtils.convertLongToString(studentCount.getDocAttitudeScores()));
  •   data.put("guideServicesScores", CommentUtils.convertLongToString(studentCount.getGuideServiceScores()));
  •   data.put("treatEffectCount", CommentUtils.convertLongToString(studentCount.getTreatEffectCount()));
  •   data.put("treatEffectScores", CommentUtils.convertLongToString(studentCount.getTreatEffectScores()));
  •   data.put("waitingTimeScores", CommentUtils.convertLongToString(studentCount.getWaitingTimeScores()));
  •   try {
  •   ops.putAll(data);
  •   } catch (Exception e) {
  •   logger.error(CommentConstants.WRITE_EXPERT_COMMENT_COUNT_REDIS_ERROR + studentCount.studentCount(), e);
  •   }
  第二、 取出
  [java] view plaincopy

  •   public StudentCountDO getStudentCommentCountInfo(String studentId) {
  •   final String strkey = "student:"+ studentId;
  •   return getTemplate().execute(new RedisCallback() {
  •   @Override
  •   public StudentCountDO doInRedis(RedisConnection connection) throws DataAccessException {
  •   byte[] bkey = getTemplate().getStringSerializer().serialize(strkey);
  •   if (connection.exists(bkey)) {
  •   List value = connection.hMGet(bkey,
  •   getTemplate().getStringSerializer().serialize("studentId"), getTemplate()
  •   .getStringSerializer().serialize("commentHeadCount"), getTemplate()
  •   .getStringSerializer().serialize("docAttitudeScores"), getTemplate()
  •   .getStringSerializer().serialize("guideServicesScores"), getTemplate()
  •   .getStringSerializer().serialize("treatEffectCount"), getTemplate()
  •   .getStringSerializer().serialize("treatEffectScores"), getTemplate()
  •   .getStringSerializer().serialize("waitingTimeScores"));
  •   StudentCountDO studentCommentCountDO = new StudentCountDO();
  •   studentCommentCountDO.setExpertId(getTemplate().getStringSerializer().deserialize(value.get(0)));
  •   studentCommentCountDO.setCommentHeadCount(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(1))));
  •   studentCommentCountDO.setDocAttitudeScores(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(2))));
  •   studentCommentCountDO.setGuideServiceScores(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(3))));
  •   studentCommentCountDO.setTreatEffectCount(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(4))));
  •   studentCommentCountDO.setTreatEffectScores(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(5))));
  •   studentCommentCountDO.setWaitingTimeScores(Long.parseLong(getTemplate().getStringSerializer()
  •   .deserialize(value.get(6))));
  •   return studentCommentCountDO;
  •   }
  •   return null;
  •   }
  •   });
  •   }
  这个存和取的过程其实是把对象中的各个字段序列化之后存入到hashmap 、取出来的时候在进行按照存入进去的顺序进行取出。
  第三 清除
  这个就根据前面的前缀很简单了,一句代码就搞定啦!
  [java] view plaincopy

  •   private void clear(String pattern) {
  •   StringRedisTemplate template = getTemplate();
  •   Set keys = template.keys(pattern);
  •   if (!keys.isEmpty()) {
  •   template.delete(keys);
  •   }
  •   }
  pattern传入为student: 就可以将该类型的所有缓存清除掉喽!
  版权声明:本文为博主原创文章,未经博主允许不得转载。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-631304-1-1.html 上篇帖子: 【Redis】Redis学习笔记02_使用Redis来管理Tomcat集群的Session 下篇帖子: redis启动时警告信息去除及简单安全设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表