SpringRedis
来源:互联网 发布:python netsnmp 编辑:程序博客网 时间:2024/05/22 07:08
Spring RedisTemplate
一、Redis介绍
什么是Redis?
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
它有什么特点?
(1)Redis数据库完全在内存中,使用磁盘仅用于持久性。
(2)相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。
(3)Redis可以将数据复制到任意数量的从服务器。
Redis 优势?
(1)异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录。
(2)支持丰富的数据类型:Redis支持最大多数开发人员已经知道像列表,集合,有序集合,散列数据类型。这使得它非常容易解决各种各样的问题,因为我们知道哪些问题是可以处理通过它的数据类型更好。
(3)操作都是原子性:所有Redis操作是原子的,这保证了如果两个客户端同时访问的Redis服务器将获得更新后的值。
(4)多功能实用工具:Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列使用(Redis原生支持发布/订阅),任何短暂的数据,应用程序,如Web应用程序会话,网页命中计数等。
Redis 缺点?
(1)单线程
(2)耗内存
命令可以去《Redis命令中心》查看 http://www.redis.cn/commands.html
jedis三种redis工作模式的连接方式
1、单机模式
2、分片模式
3、集群模式
Redis连接工厂有以下几种:- JedisConnectionFactory
- JRedisConnectionFactory
- LettuceConnectionFctory
- SrpConnectionFactory
二、Spring项目中集成Redis
- 导入jar包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version></dependency>
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.2.RELEASE</version></dependency>
- 新建redis配置文件 redis.properties
- #选择使用集群还是单台修改类型cluster/single
#选择使用集群还是单台修改类型cluster/singleredis.blacklist.type=clusterredis.blacklist.cluster.url=10.200.2.*:6379,10.200.2.*:6380,10.200.2.*:6381,10.200.2.*:6382,10.200.2.*:6383,10.200.2.*:6384redis.blacklist.single.url=192.168.0.*:12200
- 配置Bean spring-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!-- 用于存取黑名单,业务用Redis --> <bean id="redisBlacklistTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisBlacklistConnectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean> <!-- 用于存取令牌,非业务用Redis --> <bean id="redisTokenTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="redisTokenConnectionFactory"/> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> </property> </bean></beans>
- applicationContext.xml 引入配置属性文件 (其他配置省略)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" 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.xsd"> <!--引入配置属性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> </list> </property> </bean> <import resource="spring-redis.xml"/></beans>
三、配置类&工具类
package *;import com.google.common.collect.Sets;import org.apache.commons.lang3.StringUtils;import org.redisson.Redisson;import org.redisson.api.RedissonClient;import org.redisson.config.ClusterServersConfig;import org.redisson.config.Config;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisClusterConfiguration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.RedisNode;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import java.util.Set;@Configurationpublic class RedisConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class); @Value("${redis.blacklist.type}") private String blacklistType; @Value("${redis.blacklist.cluster.url}") private String REDIS_BLACKLIST_CLUSTER_URL; @Value("${redis.blacklist.single.url}") private String REDIS_BLACKLIST_SINGLE_URL; @Value("${redis.token.type}") private String tokenType; @Value("${redis.token.cluster.url}") private String REDIS_TOKEN_CLUSTER_URL; @Value("${redis.token.single.url}") private String REDIS_TOKEN_SINGLE_URL; @Bean(name = "redisBlacklistNodes") public Iterable<RedisNode> getRedisBlacklistNodes() { return getRedisNodes(REDIS_BLACKLIST_CLUSTER_URL); } @Bean(name = "redisTokenNodes") public Iterable<RedisNode> getRedisTokenNodes() { return getRedisNodes(REDIS_TOKEN_CLUSTER_URL); } private Iterable<RedisNode> getRedisNodes(String clutterUrl) { if(StringUtils.isBlank(clutterUrl)) { LOGGER.warn("REDIS配置文件为空"); return null; } String[] urls = StringUtils.split(clutterUrl, ','); Set<RedisNode> nodes = Sets.newHashSetWithExpectedSize(urls.length); for (String url : urls) { String[] splits = url.split(":"); nodes.add(new RedisNode(splits[0], Integer.parseInt(splits[1]))); } return nodes; } @Bean(name = "redisBlacklistClusterConfiguration") public RedisClusterConfiguration getBlacklistClusterConfiguration(@Qualifier("redisBlacklistNodes") Iterable<RedisNode> redisNodes) { return getRedisClusterConfiguration(redisNodes); } @Bean(name = "redisTokenClusterConfiguration") public RedisClusterConfiguration getTokenClusterConfiguration(@Qualifier("redisTokenNodes") Iterable<RedisNode> redisNodes) { return getRedisClusterConfiguration(redisNodes); } private RedisClusterConfiguration getRedisClusterConfiguration(Iterable<RedisNode> redisNodes) { if(redisNodes == null) { return null; } RedisClusterConfiguration configuration = new RedisClusterConfiguration(); configuration.setClusterNodes(redisNodes); configuration.setMaxRedirects(5); return configuration; } @Bean(name = "redisBlacklistConnectionFactory") public RedisConnectionFactory getRedisBlacklistConnectionFactory( @Qualifier("redisBlacklistClusterConfiguration") RedisClusterConfiguration redisClusterConfiguration) { return getRedisConnectionFactory(blacklistType, REDIS_BLACKLIST_SINGLE_URL, redisClusterConfiguration); } @Bean(name = "redisTokenConnectionFactory") public RedisConnectionFactory getRedisTokenConnectionFactory( @Qualifier("redisTokenClusterConfiguration") RedisClusterConfiguration redisClusterConfiguration) { return getRedisConnectionFactory(tokenType, REDIS_TOKEN_SINGLE_URL, redisClusterConfiguration); } private RedisConnectionFactory getRedisConnectionFactory(String type, String singleURL, RedisClusterConfiguration redisClusterConfiguration) { if("single".equals(type)) { if(StringUtils.isEmpty(singleURL) || !singleURL.contains(":")) { throw new RuntimeException("请检查redis配置,单点配置不正确"); } String[] split = singleURL.split(":"); return new LettuceConnectionFactory(split[0], Integer.parseInt(split[1])); } if("cluster".equals(type)) { if(redisClusterConfiguration == null) { throw new RuntimeException("请检查redis配置,集群配置不正确"); } return new LettuceConnectionFactory(redisClusterConfiguration); } return null; } @Bean(name = "redissionConfig") public Config getRedissionConfig() { if("single".equals(tokenType)) { if(StringUtils.isEmpty(REDIS_TOKEN_SINGLE_URL) || !REDIS_TOKEN_SINGLE_URL.contains(":")) { throw new RuntimeException("请检查redis配置,单点配置不正确"); } Config config = new Config(); config.useSingleServer().setAddress(REDIS_TOKEN_SINGLE_URL); return config; } if("cluster".equals(tokenType)) { if(StringUtils.isBlank(REDIS_TOKEN_CLUSTER_URL)) { throw new RuntimeException("请检查redis配置,集群配置不正确"); } Config config = new Config(); ClusterServersConfig clusterConfig = config.useClusterServers() .setScanInterval(2000); // 集群状态扫描间隔时间,单位是毫秒 String[] urls = StringUtils.split(REDIS_TOKEN_CLUSTER_URL, ','); clusterConfig.addNodeAddress(urls); return config; } return null; } @Bean(name = "redissonClient") public RedissonClient getRedissonClient(Config config) { if(config == null) { throw new RuntimeException("请检查redis配置,Redisson配置不正确"); } return Redisson.create(config); }}
package *;import java.io.Serializable;public class CacheKey implements Serializable { private String name; private String key; public static CacheKey create(String key) { return new CacheKey(key); } public static CacheKey create(String name, String key) { return new CacheKey(name, key); } public CacheKey(String name, String key) { this.name = name; this.key = key; } public CacheKey(String key) { this.key = key; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getKey() { return key; } public void setKey(String key) { this.key = key; }}
package *;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.Assert;import java.util.Set;import java.util.concurrent.TimeUnit;@Componentpublic class RedisCache { @Autowired @Qualifier("redisBlacklistTemplate") RedisTemplate<String, Object> redisTemplate; public Object get(CacheKey cacheKey) { Assert.hasText(cacheKey.getKey(), "缓存KEY不能为空"); if(StringUtils.isEmpty(cacheKey.getName())) { return redisTemplate.opsForValue().get(cacheKey.getKey()); } else { return redisTemplate.opsForHash().get(cacheKey.getName(), cacheKey.getKey()); } } public void put(CacheKey cacheKey, Object result) { Assert.hasText(cacheKey.getKey(), "缓存KEY不能为空"); if(StringUtils.isEmpty(cacheKey.getName())) { redisTemplate.opsForValue().set(cacheKey.getKey(), result); } else { redisTemplate.opsForHash().put(cacheKey.getName(), cacheKey.getKey(), result); } } public void put(CacheKey cacheKey, Object result, int second) { Assert.hasText(cacheKey.getKey(), "缓存KEY不能为空"); if(StringUtils.isEmpty(cacheKey.getName())) { redisTemplate.opsForValue().set(cacheKey.getKey(), result, second, TimeUnit.SECONDS); } else { redisTemplate.opsForHash().put(cacheKey.getName(), cacheKey.getKey(), result); redisTemplate.expire(cacheKey.getName(), second, TimeUnit.SECONDS); } } public void evict(CacheKey cacheKey) { Assert.hasText(cacheKey.getKey(), "缓存KEY不能为空"); if(StringUtils.isEmpty(cacheKey.getName())) { redisTemplate.delete(cacheKey.getKey()); } else { redisTemplate.opsForHash().delete(cacheKey.getName(), cacheKey.getKey()); } } public void putSet(CacheKey cacheKey, Object value) { if(StringUtils.isEmpty(cacheKey.getName())) { redisTemplate.opsForSet().add(cacheKey.getName(), value); } else { redisTemplate.opsForHash().put(cacheKey.getName(), cacheKey.getKey(), value); } } /** * SET ADD * @see http://redis.io/commands/sadd * @param cacheKey * @param value */ public void sadd(CacheKey cacheKey, Object value) { redisTemplate.opsForSet().add(cacheKey.getKey(), value); } public void srem(CacheKey cacheKey, Object value) { redisTemplate.opsForSet().remove(cacheKey.getKey(), value); } public boolean sismember(CacheKey cacheKey, Object value) { return redisTemplate.opsForSet().isMember(cacheKey.getKey(), value); } public Set<Object> sget(CacheKey cacheKey) { Assert.hasText(cacheKey.getKey(), "缓存KEY不能为空"); return redisTemplate.opsForSet().members(cacheKey.getKey()); } public void removeAll(CacheKey cacheKey) { redisTemplate.delete(cacheKey.getKey()); }}
在其他类中使用:
@Autowired@Qualifier("redisTokenTemplate")RedisTemplate<String, Object> redisTemplate;private List<ApiMethod> queryAll(boolean forceInit) { List<ApiMethod> apiMethodList; if(forceInit) { //从缓存中获取 apiMethodList = (List<ApiMethod>) redisTemplate.opsForValue().get(EnumCacheKey.API_METHOD_ALL.getKey()); if(apiMethodList != null) { return apiMethodList; } } Paging<ApiMethod, ApiMethodRequest> page = new Paging<ApiMethod, ApiMethodRequest>(); page.setSortRow("id"); page.setSortMethod("ASC"); page.setPageSize(Constants.QUERY_API_METHOD_COUNT); apiMethodList = queryPaging(page).getData(); apiMethodStorage.init(apiMethodList); return apiMethodList;}