重庆小潘seo博客

当前位置:首页 > 重庆网络营销 > 小潘杂谈 >

小潘杂谈

redis怎么用在项目上

时间:2020-09-23 09:40:07 作者:重庆seo小潘 来源:
想起自己以前想把Redis整合到JavaWeb项目中,网上搜了很多教程都不全面,现在我终于弄明白了,所以想在这分享一下。 1.Redis的安装 我这里就不讲了,网上有很多教程,windows,Linux,我自己的是搭建在服务器上的。 图形化连接 向业务逻辑中添加缓存 1.1. 接口

redis怎么用在项目上

想起自己以前想把Redis整合到JavaWeb项目中,网上搜了很多教程都不全面,现在我终于弄明白了,所以想在这分享一下。

redis怎么用在项目上

1.Redis的安装

我这里就不讲了,网上有很多教程,windows,Linux,我自己的是搭建在服务器上的。

图形化连接

redis怎么用在项目上

向业务逻辑中添加缓存

1.1. 接口封装

常用的操作redis的方法提取出一个接口,分别对应单机版和集群版创建两个实现类。

1.1.1. 接口定义

##jedisClientpackage cn.e3mall.common.jedis;import java.util.List;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);Boolean hexists(String key, String field);List<String> hvals(String key);Long del(String key);}###JedisClientPoolpackage cn.e3mall.common.jedis;import java.util.List;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;public class JedisClientPool implements JedisClient {private JedisPool jedisPool;public JedisPool getJedisPool() {return jedisPool;}public void setJedisPool(JedisPool jedisPool) {this.jedisPool = jedisPool;}@Overridepublic String set(String key, String value) {Jedis jedis = jedisPool.getResource();String result = jedis.set(key, value);jedis.close();return result;}@Overridepublic String get(String key) {Jedis jedis = jedisPool.getResource();String result = jedis.get(key);jedis.close();return result;}@Overridepublic Boolean exists(String key) {Jedis jedis = jedisPool.getResource();Boolean result = jedis.exists(key);jedis.close();return result;}@Overridepublic Long expire(String key, int seconds) {Jedis jedis = jedisPool.getResource();Long result = jedis.expire(key, seconds);jedis.close();return result;}@Overridepublic Long ttl(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.ttl(key);jedis.close();return result;}@Overridepublic Long incr(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.incr(key);jedis.close();return result;}@Overridepublic Long hset(String key, String field, String value) {Jedis jedis = jedisPool.getResource();Long result = jedis.hset(key, field, value);jedis.close();return result;}@Overridepublic String hget(String key, String field) {Jedis jedis = jedisPool.getResource();String result = jedis.hget(key, field);jedis.close();return result;}@Overridepublic Long hdel(String key, String... field) {Jedis jedis = jedisPool.getResource();Long result = jedis.hdel(key, field);jedis.close();return result;}@Overridepublic Boolean hexists(String key, String field) {Jedis jedis = jedisPool.getResource();Boolean result = jedis.hexists(key, field);jedis.close();return result;}@Overridepublic List<String> hvals(String key) {Jedis jedis = jedisPool.getResource();List<String> result = jedis.hvals(key);jedis.close();return result;}@Overridepublic Long del(String key) {Jedis jedis = jedisPool.getResource();Long result = jedis.del(key);jedis.close();return result;}}###JedisClientClusterpackage cn.e3mall.common.jedis;import java.util.List;import redis.clients.jedis.JedisCluster;public class JedisClientCluster implements JedisClient {private JedisCluster jedisCluster;public JedisCluster getJedisCluster() {return jedisCluster;}public void setJedisCluster(JedisCluster jedisCluster) {this.jedisCluster = jedisCluster;}@Overridepublic String set(String key, String value) {return jedisCluster.set(key, value);}@Overridepublic String get(String key) {return jedisCluster.get(key);}@Overridepublic Boolean exists(String key) {return jedisCluster.exists(key);}@Overridepublic Long expire(String key, int seconds) {return jedisCluster.expire(key, seconds);}@Overridepublic Long ttl(String key) {return jedisCluster.ttl(key);}@Overridepublic Long incr(String key) {return jedisCluster.incr(key);}@Overridepublic Long hset(String key, String field, String value) {return jedisCluster.hset(key, field, value);}@Overridepublic String hget(String key, String field) {return jedisCluster.hget(key, field);}@Overridepublic Long hdel(String key, String... field) {return jedisCluster.hdel(key, field);}@Overridepublic Boolean hexists(String key, String field) {return jedisCluster.hexists(key, field);}@Overridepublic List<String> hvals(String key) {return jedisCluster.hvals(key);}@Overridepublic Long del(String key) {return jedisCluster.del(key);}}配置:applicationContext-redis.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"><!-- 连接redis单机版 --><bean id="jedisClientPool"> 封装代码测试@Testpublic void testJedisClient() throws Exception {//初始化Spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");//从容器中获得JedisClient对象JedisClient jedisClient = applicationContext.getBean(JedisClient.class);jedisClient.set("first", "100");String result = jedisClient.get("first");System.out.println(result);}添加缓存

1.1.1. 功能分析

查询内容列表时添加缓存。

1、查询数据库之前先查询缓存。

2、查询到结果,直接响应结果。

3、查询不到,缓存中没有需要查询数据库。

4、把查询结果添加到缓存中。

5、返回结果。向redis中添加缓存:Key:cidValue:内容列表。需要把java对象转换成json。 使用hash对key进行归类。HASH_KEY:HASH|--KEY:VALUE|--KEY:VALUE|--KEY:VALUE|--KEY:VALUE注意:添加缓存不能影响正常业务逻辑。

代码实现(在服务层实现)@Autowiredprivate JedisClient jedisClient;@Value("${CONTENT_LIST}")private String CONTENT_LIST;@Overridepublic E3Result addContent(TbContent content) {//将内容数据插入到内容表content.setCreated(new Date());content.setUpdated(new Date());//插入到数据库contentMapper.insert(content);//缓存同步,删除缓存中对应的数据。jedisClient.hdel(CONTENT_LIST, content.getCategoryId().toString());return E3Result.ok();}@Overridepublic List<TbContent> getContentListByCid(long cid) {//查询缓存try {//如果缓存中有直接响应结果String json = jedisClient.hget(CONTENT_LIST, cid + "");if (StringUtils.isNotBlank(json)) {List<TbContent> list = JsonUtils.jsonToList(json,TbContent.class);System.out.println("从缓存中查出的数据");return list;}} catch (Exception e) {e.printStackTrace();}//如果没有查询数据库TbContentExample example = new TbContentExample();Criteria criteria = example.createCriteria();//设置查询条件criteria.andCategoryIdEqualTo(cid);//执行查询List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);//把结果添加到缓存try {System.out.println("把结果添加到缓存");jedisClient.hset(CONTENT_LIST, cid + "", JsonUtils.objectToJson(list));} catch (Exception e) {e.printStackTrace();}return list;}**注意要实现缓存同步

再讲一个东西,热缓存,给它设一个过期时间(不同步也是可以的)@Overridepublic TbItem getItemById(long itemId) {//获取商品添加缓存,不影业务响逻辑,try-catchtry {System.out.println("缓存获取商品信息");String json = jedisClient.get(REDIS_ITEM_PRE+":"+itemId+":BASE");if(StringUtils.isNotBlank(json)) {TbItem tbItem = JsonUtils.jsonToPojo(json,TbItem.class);return tbItem;}}catch(Exception e) {e.printStackTrace();}//缓存中没有,查询数据库//根据主键查询//TbItem tbItem = itemMapper.selectByPrimaryKey(itemId);TbItemExample example = new TbItemExample();Criteria criteria = example.createCriteria();//设置查询条件criteria.andIdEqualTo(itemId);//执行查询List<TbItem> list = itemMapper.selectByExample(example);if (list != null && list.size() > 0) {//结果添加到缓存try {System.out.println("缓存添加商品信息");jedisClient.set(REDIS_ITEM_PRE+":"+itemId+":BASE",JsonUtils.objectToJson(list.get(0)));//设置过期时间(1个小时)jedisClient.expire(REDIS_ITEM_PRE+":"+itemId+":BASE",TIEM_CACHE_EXPIRE);}catch(Exception e) {e.printStackTrace();}return list.get(0);}return null;}redis怎么用在项目上

redis怎么用在项目上

redis中的数据:{"itemId":1231490,"created":1425821627000,"updated":1425821627000,"itemDesc":"<table cellpadding="0" cellspacing="0" align="center" border="0" width="750"><tbody><tr><td height="51" width="750"><div style="PADDING-BOTTOM: 15px; LINE-HEIGHT: 1.5em; PADDING-LEFT: 15px; PADDING-RIGHT: 15px; FONT-FAMILY: '微软雅黑'; FONT-SIZE: 15px; PADDING-TOP: 15px"><span style="color:#ff6666;"><strong>温馨提示:</strong></span><strong>小米4联通版</strong>内置运营商软件,<strong>小米4官方联通版</strong>是无内置运营商软件的。</div></td></tr></tbody></table><div>更多Redis相关技术文章,请访问Redis教程栏目进行学习!以上就是redis怎么用在项目上的详细内容,更多请关注小潘博客其它相关文章!