在项目开发中使用Redis作缓存
出自https://blog.csdn.net/chenhaotao/article/details/78491558
Redis是nosql非关系型数据库,数据结构以key-value的形式,每个key对应唯一一个value,同时由于Redis数据是运行在内存中的,查询效率非常高,但是又因为是直接占用内存资源的,一般只对高频率的数据进行缓存。
在项目中使用Redis缓存原因:
1.减轻数据库压力
2.提高查询效率
1 Redis的数据结构为key-value,其中value支持五种格数数据: 2 1.字符串(strings) 3 2.字符串列表(lists) 4 3.字符串集合(sets) 5 4.有序字符串集合(sorted sets) 6 5.哈希(hashes) 7 8 9 10 在项目中使用Redis缓存流程: 11 1.查询时先从缓存中查询 12 2.缓存中如果没有数据再从数据库查询,并将数据保存进缓存 13 3.如果缓存中查询到数据,直接返回,不再需要查询数据库
数据的缓存同步问题
如果对数据进行了缓存,当查询数据时,如果缓存中有数据则直接返回缓存数据不会查询数据,当数据库数据改变时就有可能出现数据不一致的问题。可以考虑在每次修改数据库的时候同时将对应的缓存数据删除,这样重新查询的时候就会查询数据库并缓存(针对数据量比较小的情况)。
spring整合Redis
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- redis交给spring管理 --> <!-- 创建JedisPoolConfig对象 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000"></property> <property name="maxIdle" value="20"></property> </bean> <!-- 单机版连接池交给spring管理 --> <bean class="redis.clients.jedis.JedisPool"> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="6379"></constructor-arg> </bean> <!-- 集群交给spring管理 --> <!-- <bean class="redis.clients.jedis.JedisCluster"> <constructor-arg name="nodes"> <set> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7001"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7002"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7003"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7004"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7005"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7006"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7007"></constructor-arg> </bean> <bean class="redis.clients.jedis.HostAndPort"> <constructor-arg name="host" value="192.168.66.66"></constructor-arg> <constructor-arg name="port" value="7008"></constructor-arg> </bean> </set> </constructor-arg> <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg> </bean> --> </beans>
jedis接口抽取
1 public interface JedisDao { 2 3 //抽取jedis访问redis服务器常用方法 4 //String 5 public String set(String key,String value); 6 public String get(String key); 7 //自增 8 public Long incr(String key); 9 //自减 10 public Long decr(String key); 11 //过期时间 12 public Long expire(String key, int seconds); 13 //查询过期剩余时间 14 public Long ttl(String key); 15 //HASH 16 public Long hset(String key, String field, String value); 17 public String hget(String key, String field); 18 //删除 19 public Long hdel(String key, String field); 20 21 }
单机版Redis接口实现类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import cn.e3mall.content.dao.JedisDao; @Repository public class SingleJedisDaoImpl implements JedisDao { //注入单机版jedis连接池对象 @Autowired private JedisPool jp; @Override public String set(String key, String value) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); String set = jedis.set(key, value); return set; } @Override public String get(String key) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); String value = jedis.get(key); return value; } @Override public Long incr(String key) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long incr = jedis.incr(key); return incr; } @Override public Long decr(String key) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long decr = jedis.decr(key); return decr; } @Override public Long expire(String key, int seconds) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long expire = jedis.expire(key, seconds); return expire; } @Override public Long ttl(String key) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long ttl = jedis.ttl(key); return ttl; } @Override public Long hset(String key, String field, String value) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long hset = jedis.hset(key, field, value); return hset; } @Override public String hget(String key, String field) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); String hget = jedis.hget(key, field); return hget; } @Override public Long hdel(String key, String field) { // TODO Auto-generated method stub Jedis jedis = jp.getResource(); Long hdel = jedis.hdel(key, field); return hdel; } }
集群版Redis接口实现类
1 import org.springframework.beans.factory.annotation.Autowired; 2 import org.springframework.stereotype.Repository; 3 4 import redis.clients.jedis.JedisCluster; 5 import cn.e3mall.content.dao.JedisDao; 6 @Repository 7 public class ClusterJedisDaoImpl implements JedisDao { 8 9 //注解集群对象 10 @Autowired 11 private JedisCluster jc; 12 13 @Override 14 public String set(String key, String value) { 15 // TODO Auto-generated method stub 16 String set = jc.set(key, value); 17 return set; 18 } 19 20 @Override 21 public String get(String key) { 22 // TODO Auto-generated method stub 23 String string = jc.get(key); 24 return string; 25 } 26 27 @Override 28 public Long incr(String key) { 29 // TODO Auto-generated method stub 30 Long incr = jc.incr(key); 31 return incr; 32 } 33 34 @Override 35 public Long decr(String key) { 36 // TODO Auto-generated method stub 37 Long decr = jc.decr(key); 38 return decr; 39 } 40 41 @Override 42 public Long expire(String key, int seconds) { 43 // TODO Auto-generated method stub 44 Long expire = jc.expire(key, seconds); 45 return expire; 46 } 47 48 @Override 49 public Long ttl(String key) { 50 // TODO Auto-generated method stub 51 Long ttl = jc.ttl(key); 52 return ttl; 53 } 54 55 @Override 56 public Long hset(String key, String field, String value) { 57 // TODO Auto-generated method stub 58 Long hset = jc.hset(key, field, value); 59 return hset; 60 } 61 62 @Override 63 public String hget(String key, String field) { 64 // TODO Auto-generated method stub 65 String value = jc.hget(key, field); 66 return value; 67 } 68 69 @Override 70 public Long hdel(String key, String field) { 71 // TODO Auto-generated method stub 72 Long hdel = jc.hdel(key, field); 73 return hdel; 74 } 75 76 }
Redis集群思想
备份(高可用)
分块(高效率、高并发)