直接进入主题 :
: ServiceImpl : :
@Service public class jedisclientsingleservice реализует jedisclient {@autowired private jedispool jedispool; @override public String get (String Key) {jedis jedis = jedispool.getResource (); jedis = jedispool.getresource (); string string = jedis.set (keid, value); jedis.close (); return String;}@overde public String hget (String hkey, String Key) {jedis jedis = jedispool.getresource (); string string = jedis.hget (hkey, key); hset (String hkey, String Key, String value) {jedis jedis = jedispool.getresource (); long result = jedis.hset (hkey, key, value); jedis.close (); return result;}@переопределить публичный длинный ровный jedis.incr (key); jedis.close (); return result;}@переопределить публичный длинный срок действия (String Key, int Second) {jedis jedis = jedispool.getresource (); long result = jedis.expire (ключ, string); jedis.close (); returant result;}@overde public wuted ttl ttl (string key) {jedis jedis = jedis jedis = jedis = jedis = jedis = jedis jedis = jedis jedis jedis jedis jedis jedis. jedispool.getresource (); long result = jedis.ttl (key); jedis.close (); return result;}@переопределить публичный Long del (String Key) {jedis jedis = jedispool.getresource ();@jedis.del (key); jedis.close (); вернуть результат; {Jedis jedis = jedispool.getresource (); long result = jedis.hdel (hkey, key); jedis.close (); return result;}: : 添加缓存出 (一般写在 Сервис 是层中) :
Общедоступный список if (! stringutils.isblank (cachstring)) {list <roleresource> list = jsonutils.jsonstrtolist (cachstring, roleresource.class); вернуть список; }} catch (Exception e) {e.printstackTrace (); } List <Roleresource> list = sessionFactory.opensession (). SelectList ("cn.sys.auth.entity.resourcesmapper.getTreeGrid"); // 将缓存中添加缓存 try {// redsi 只存字符串 , 把 List 转换换成字符串 String cachstring = jsonutils.tojson (list); jedisclientsingleservice.hset (all_resources_no_condition, hashid, cachstring); } catch (Exception e) {e.printstackTrace (); } return List; }: : , 如果缓存中没有 , 就去数据库中取 , 然后把数据存入缓存 下次查询时就会从缓存中取。 下次查询时就会从缓存中取。
三 : 缓存的同步
问题来了 , , 下次取的时候 , 因为缓存中有数据便在缓存中取 , 这是数据库的数据与缓存中的数据不一致 便出现差异 , 这就要缓存同步了。 这就要缓存同步了。
其实很简单 , 就是在修改 删除 (如果添加也需要的话) , 执行下面操作 :
1 : 删除缓存 处理数据 , 把数据放如缓存
2 : 删除缓存 , (等查询数据的时候会把数据放入缓存 , 两种情况只是写缓存时间的区别)
try {jedisclientsingleservice.hdel (all_resources_no_condition, hasshid); } catch (Exception e) {e.printstackTrace (); }总结
以上就是本文关于 javaweb 中使用 redis 缓存实例解析的全部内容 , 希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题 如有不足之处 , 欢迎留言指出。感谢朋友们对本站的支持! 欢迎留言指出。感谢朋友们对本站的支持!