直接进入主题
一 : ServiceImpl 定义:
@Service Public class JedIclientsingLeService implementa JEDISCLIENT {@AUTOWIRED PRIVADO JEDISPOOL JEDISPOOL; @Override public String get (key string) {jedis jedis = jedispool.getResource (); string string = jedis.get (key); JedisPool.Close (); Jedis = jedispool.getResource (); string string = jedis.set (key, value); jedis.close (); return string;}@substituir public string hget (string hkey, string key) {jedis jedis = jedispool.GeReSource (); string string = jedis.hget (hkey); hset (string hkey, chave de string, valor da string) {jedis jedis = jedispool.getResource (); resultado longo = jedis.hset (hkey, key, value); jedis.close (); return result;}@substituir public incr (string key) {jedis jedis = jedispool.GeRSORRSOURCE (string key) {jedis jedis = jedispool.GeRSORRSOURCE (string string) {jedis jedis = jedispool.GeRSORRSOURCE (string string) {jedis jedis = jedispool.GeRSORRSOURCE (string string) {JEDIS Jedis = jedispool.GeRSOrSOrSOURC jedis.Irnc (key); jedis.close (); resultado de retorno;}@substituir public long expirar (chave de string, int segundo) {jedis jedis = jedispool.getResource (); resultado de jedis = jiDis.expire (key, segundo); jEdis.close (); resultado; jedispool.getResource (); resultado longo = jedis.ttl (key); jedis.close (); resultado de retorno;}@substituir public long del (string key) {jedis jedis = jedispool.getResource (); resultado long = jedis.del (key); hdisel.close (); {Jedis Jedis = Jedispool.getResource (); Long resultado = Jedis.hdel (hkey, key); jedis.close (); resultado de retorno;}二 : 添加缓存出 (一般写在 Serviço 是层中)) ::
Lista pública <rolereResource> getTreegrid () {// 从缓存中获取内容 Try {String CachString = JEDISCLIENTESINGLESERVICE.HGET (ALL_RESOURCES_NO_CONDITION, HASHID); if (! stringUtils.isblank (Cachstring)) {list <rolereresource> list = jsonutils.jSonstrtolist (Cachstring, roleResource.class); lista de retorno; }} catch (Exceção e) {e.printStackTrace (); } List <rolereReSource> list = sessionFactory.opensession (). SelectList ("cn.sys.auth.entity.resourcesmapper.getTreeGrid"); // 将缓存中添加缓存 tente {// Redsi 只存字符串 , 把 Lista 转换换成字符串 String Cachstring = jsonutils.tojson (lista); JedIclientsingleService.hset (all_resources_no_condition, hashid, cachstring); } catch (Exceção e) {e.printStackTrace (); } Lista de retorno; }逻辑 : 先从缓存中取数据 , 如果缓存中没有 , 就去数据库中取 , 然后把数据存入缓存 , 下次查询时就会从缓存中取。
三: 缓存的同步
问题来了 , 入过你修改或者删除了数据 , , , 因为缓存中有数据便在缓存中取 , , , , 这就要缓存同步了。 这就要缓存同步了。
其实很简单 , 就是在修改 , 删除 (如果添加也需要的话) , 执行下面操作 ::
1 : 删除缓存 , 处理数据 , 把数据放如缓存
2 : 删除缓存 , 处理数据 (等查询数据的时候会把数据放入缓存 , 两种情况只是写缓存时间的区别))))))))))))) 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别 两种情况只是写缓存时间的区别
tente {jedisclientsingleService.hdel (all_resources_no_condition, hashid); } catch (Exceção e) {e.printStackTrace (); }总结
以上就是本文关于 Javaweb 中使用 Redis 缓存实例解析的全部内容 , 希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题 , 如有不足之处 , 欢迎留言指出。感谢朋友们对本站的支持!