javaWeb中使用Redis缓存实例解析

时间:2021-08-05 02:38:46

直接进入主题:

一:serviceImpl定义:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@Service
  public class JedisClientSingleService implements JedisClient {
    @Autowired
        private JedisPool jedisPool;
    @Override
        public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.get(key);
        jedis.close();
        return string;
    }
    @Override
        public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.set(key, value);
        jedis.close();
        return string;
    }
    @Override
        public String hget(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        String string = jedis.hget(hkey, key);
        jedis.close();
        return string;
    }
    @Override
        public long hset(String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.hset(hkey, key, value);
        jedis.close();
        return result;
    }
    @Override
        public long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.incr(key);
        jedis.close();
        return result;
    }
    @Override
        public long expire(String key, int second) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.expire(key, second);
        jedis.close();
        return result;
    }
    @Override
        public long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.ttl(key);
        jedis.close();
        return result;
    }
    @Override
        public long del(String key) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.del(key);
        jedis.close();
        return result;
    }
    @Override
        public long hdel(String hkey, String key) {
        Jedis jedis = jedisPool.getResource();
        long result = jedis.hdel(hkey, key);
        jedis.close();
        return result;
    }

二:添加缓存出(一般写在service是层中):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public List<RoleResource> getTreeGrid() {
    //从缓存中获取内容
    try {
      String cachString = jedisClientSingleService.hget(ALL_RESOURCES_NO_CONDITION, hashId);
      if(!StringUtils.isBlank(cachString)){
        List<RoleResource> list = JsonUtils.jsonStrToList(cachString, RoleResource.class) ;
        return list ;
      }
    } 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:删除缓存,处理数据(等查询数据的时候会把数据放入缓存,两种情况只是写缓存时间的区别)

?
1
2
3
4
5
try {
      jedisClientSingleService.hdel(ALL_RESOURCES_NO_CONDITION, hashId);
    } catch (Exception e) {
      e.printStackTrace();
    }

总结

以上就是本文关于javaWeb中使用Redis缓存实例解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/j_bang/article/details/53264758