rdis 缓存
来源:互联网 发布:java实战项目百度云 编辑:程序博客网 时间:2024/06/03 12:54
@Servicepublic class NoticeServiceImpl implements NoticeService{@Autowiredprivate NoticeMapper noticeMapper;@Autowiredprivate RedisService redisService;private static final StringHOTS = "HOTS";/** 查询不同新闻类型下的新闻资讯 */public List<New> queryNewsByNewType(String NewType,Integer Count) { if (NewType.equals(types)) { //先命中 String jsonData = this.redisService.get(HOTS); if (!StringUtils.isEmpty(jsonData)) { //命中 try { List<New> list = MAPPER.readValue(jsonData,List.class); return list; } catch (Exception e) { e.printStackTrace(); } } List<New> list = noticeMapper.queryNewsByNewType(NewType, Count); //加入redis缓存中 try { this.redisService.setExpire(HOTS, MAPPER.writeValueAsString(list), 60*60*24); } catch (Exception e) { e.printStackTrace(); } return list; } List<New> list = noticeMapper.queryNewsByNewType(NewType, Count); return list;}}package com.qiantufax.manage.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.qiantufax.manage.service.Function;import com.qiantufax.manage.service.RedisService;import redis.clients.jedis.ShardedJedis;import redis.clients.jedis.ShardedJedisPool;@Servicepublic class RedisServiceImpl implements RedisService{ @Autowired(required = false)//使用到时才注册该对象 private ShardedJedisPool shardedJedisPool; /** * 执行set * @return */ public <T> T excute(Function<ShardedJedis, T> fun){ ShardedJedis shardedJedis = null; try { // 从连接池中获取到jedis分片对象 shardedJedis = shardedJedisPool.getResource(); return fun.callback(shardedJedis); } catch (Exception e) { e.printStackTrace(); } finally { if (null != shardedJedis) { // 关闭,检测连接是否有效,有效则放回到连接池中,无效则重置状态 shardedJedis.close(); } } return null; } /** * 执行set */ public String set(final String key,final String value){ return this.excute(new Function<ShardedJedis, String>() { @Override public String callback(ShardedJedis e) { return e.set(key, value); } }); } /** * 执行get */ public String get(final String key){ return this.excute(new Function<ShardedJedis, String>() { @Override public String callback(ShardedJedis e) { return e.get(key); } }); } /** * 设置生存时间 */ public Long expire(final String key,final Integer seconds){ return this.excute(new Function<ShardedJedis, Long>(){ @Override public Long callback(ShardedJedis e) { Long expire = e.expire(key, seconds); return expire; } }); } /** * 设置key和value并设置生存时间 */ public Long setExpire(final String key,final String value,final Integer seconds){ return this.excute(new Function<ShardedJedis, Long>(){ @Override public Long callback(ShardedJedis e) { e.set(key, value); return e.expire(key, seconds); } }); } /** * 删除键 */ public Long del(final String key){ return this.excute(new Function<ShardedJedis, Long>(){ @Override public Long callback(ShardedJedis e) { return e.del(key); } }); } }
0 0
- rdis 缓存
- Rdis
- Rdis 基础知识
- rdis+window+php 安装
- Rdis快速入门
- rdis和memcache的区别
- rdis-py 2.4.13 documents
- Rdis客户端开发API包的介绍
- 47、Rdis学习(CentOS下安装Centos 2017年3月)
- 缓存
- 缓存
- 缓存
- 缓存
- 缓存
- 缓存
- 缓存
- 缓存
- 缓存
- linux wc命令学习
- 链表的有序集合
- 最短路练习5/poj/1860 /Currency Exchange 有正环
- oracle数据库表空间.DBF文件被手动删除(直接找),然后就登录不了数据库的解决办法
- Linux入门(5)——Ubuntu16.04安装网易云音乐
- rdis 缓存
- 我理解的MVC开发模式
- 2017Emacs
- collectionView 滚动到制定位置
- 了解c++中的类型转换
- java 5种设计模式
- How the backpropagation algorithm works
- Linux入门(6)——Ubuntu16.04安装atom
- html