一、在pom文件中添加依赖
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>3.0.1</version> </dependency>
二、准备JedisClient接口
public interface JedisClient { String set(String key, String value); String get(String key); Boolean exists(String key); Long expire(String key, int seconds); Long ttl(String key); Long incr(String key); Long hset(String key, String field, String value); String hget(String key, String field); Long hdel(String key, String... field); }
三、实现
1.单机版
public class JedisClientPool implements JedisClient { @Autowired private JedisPool jedisPool; @Override public String set(String key, String value) { Jedis jedis = jedisPool.getResource(); String result = jedis.set(key, value); jedis.close(); return result; } @Override public String get(String key) { Jedis jedis = jedisPool.getResource(); String result = jedis.get(key); jedis.close(); return result; } @Override public Boolean exists(String key) { Jedis jedis = jedisPool.getResource(); Boolean result = jedis.exists(key); jedis.close(); return result; } @Override public Long expire(String key, int seconds) { Jedis jedis = jedisPool.getResource(); Long result = jedis.expire(key, seconds); 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 incr(String key) { Jedis jedis = jedisPool.getResource(); Long result = jedis.incr(key); jedis.close(); return result; } @Override public Long hset(String key, String field, String value) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hset(key, field, value); jedis.close(); return result; } @Override public String hget(String key, String field) { Jedis jedis = jedisPool.getResource(); String result = jedis.hget(key, field); jedis.close(); return result; } @Override public Long hdel(String key, String... field) { Jedis jedis = jedisPool.getResource(); Long result = jedis.hdel(key, field); jedis.close(); return result; } }
(2)集群版
public class JedisClientCluster implements JedisClient { @Autowired private JedisCluster jedisCluster; @Override public String set(String key, String value) { return jedisCluster.set(key, value); } @Override public String get(String key) { return jedisCluster.get(key); } @Override public Boolean exists(String key) { return jedisCluster.exists(key); } @Override public Long expire(String key, int seconds) { return jedisCluster.expire(key, seconds); } @Override public Long ttl(String key) { return jedisCluster.ttl(key); } @Override public Long incr(String key) { return jedisCluster.incr(key); } @Override public Long hset(String key, String field, String value) { return jedisCluster.hset(key, field, value); } @Override public String hget(String key, String field) { return jedisCluster.hget(key, field); } @Override public Long hdel(String key, String... field) { return jedisCluster.hdel(key, field); }
四、配置文件
<!-- redis单机版 --> <!-- <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg name="host" value="192.168.206.132"/> <constructor-arg name="port" value="7001"/> </bean> <bean id="jedisClientPool" class="com.taotao.jedis.JedisClientPool"/> --> <!-- redis集群 --> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"> <constructor-arg> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.206.132"/> <constructor-arg name="port" value="7001"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.132"/> <constructor-arg name="port" value="7002"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.25.132"/> <constructor-arg name="port" value="7003"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.206.132"/> <constructor-arg name="port" value="7004"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.206.132"/> <constructor-arg name="port" value="7005"/> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.206.132"/> <constructor-arg name="port" value="7006"/> </bean> </set> </constructor-arg> </bean> <bean id="jedisClientCluster" class="com.taotao.jedis.JedisClientCluster"/>
五、测试
@Test public void testJedis(){ //初始化spring容器 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml"); //从容器中获得jedisclient对象 JedisClient jedisClient = applicationContext.getBean(JedisClient.class); //使用JedisClient操作redis jedisClient.set("ok", "test666"); String result = jedisClient.get("ok"); System.out.println(result); }
六、向业务逻辑中添加缓存
注:添加缓存不能影响正常的业务逻辑
查询时,业务中应该先去缓存中查询
如果缓存中没有,在数据库查询
将数据库查询到的结果添加到缓存中
推荐使用的数据类型为hash
如下代码:
public List<TbContent> getContentList(long cid) { //查询缓存 try { String json = jedisClient.hget(CONTENT_KEY, cid + ""); //判断json是否为空 if (StringUtils.isNotBlank(json)) { //把json转换成list List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class); return list; } } catch (Exception e) { e.printStackTrace(); } //根据cid查询内容列表 TbContentExample example = new TbContentExample(); //设置查询条件 Criteria criteria = example.createCriteria(); criteria.andCategoryIdEqualTo(cid); //执行查询 List<TbContent> list = contentMapper.selectByExample(example); //向缓存中添加数据 try { jedisClient.hset(CONTENT_KEY, cid + "", JsonUtils.objectToJson(list)); } catch (Exception e) { e.printStackTrace(); } return list; }
七、缓存同步
对内容信息做增删改操作后只需要把对应缓存删除即可
//缓存同步 jedisClient.hdel(CONTENT_KEY, content.getCategoryId().toString());
原文地址:https://www.cnblogs.com/wanerhu/p/11241631.html
时间: 2024-10-11 11:15:34