Redis是一种特殊类型的数据库,他被称之为key-value存储java
本文覆盖缓存和存储两方面进行说明,使用的是Spring 4.0和Java配置方式git
代码地址下载地址:https://github.com/zoeminghong/springmvc-javaconfiggithub
package springmvc.rootconfig; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; @Configuration @EnableCaching public class CachingConfig { /** * 链接Redis * * @return */ @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); // host地址 jedisConnectionFactory.setHostName("10.10.13.12"); // 端口号 jedisConnectionFactory.setPort(6379); jedisConnectionFactory.afterPropertiesSet(); return jedisConnectionFactory; } /** * RedisTemplate配置 * * @param redisCF * @return */ @Bean public RedisTemplate<String, Object> redisTemplate( RedisConnectionFactory redisCF) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(redisCF); redisTemplate.afterPropertiesSet(); return redisTemplate; } }
Redis链接工厂web
JedisConnectionFactoryredis
JredisConnectionFactoryspring
LettuceConnectionFactorymongodb
SrpConnectionFactory数据库
建议自行测试选用合适本身的链接工厂数组
若是使用的是localhost和默认端口,则这两项的配置能够省略缓存
RedisTemplate
RedisTemplate
StringRedisTemplate
RedisTemplate可以让咱们持久化各类类型的key和value,并不只限于字节数组
StringRedisTemplate扩展了RedisTemplate,只能使用String类型
StringRedisTemplate有一个接受RedisConnectionFactory的构造器,所以没有必要在构建后在调用setConnectionFactory()
使用RedisTemplateAPI
方法 | 子API接口 | 描述 |
---|---|---|
opsForValue() | ValueOperations<K,V> | 描述具备简单值的条目 |
opsForList() | ListOperations<K,V> | 操做具备list值的条目 |
opsForSet() | SetOperations<K,V> | 操做具备set值的条目 |
opsForZSet() | ZSetOperations<K,V> | 操做具备ZSet值(排序的set)的条目 |
opsForHash() | HashOperations<K,HK,VH> | 操做具备hash值的条目 |
boundValueOps(K) | BoundValueOperations<K,V> | 以绑定指定key的方式,操做具备简单值的条目 |
boundListOps(K) | BoundListOperations<K,V> | 以绑定指定key的方式,操做具备list的条目 |
boundSetOps(K) | BoundSetOperations<K,V> | 以绑定指定key的方式,操做具备set的条目 |
boundZSet(K) | BoundZSetOperations<K,V> | 以绑定指定key的方式,操做具备ZSet(排序的set)的条目 |
boundHashOps(K) | BoundHashOperations<K,V> | 以绑定指定key的方式,操做具备hash值的条目 |
操做
package springmvc.web; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import springmvc.bean.Order; import springmvc.orders.db.OrderRepository; @Controller public class HomeController { @Autowired RedisTemplate<String, Object> redisTemplate; @RequestMapping(value = { "/", "index" }, method = RequestMethod.GET) public String index() { redisTemplate.opsForValue().set("gege", 11); System.out.print(redisTemplate.opsForValue().get("gege")); return "index"; } }
//建立List条目,key是cart BoundListOperations<String, Object>cart=redisTemplate.boundListOps("cart"); //删除最后的一条数据 cart.rightPop(); //在最后,添加一条数据 cart.rightPush("我笑了");
若是要使用到JavaBean,须要其实现Serializable接口,将其序列化
或者使用Spring Data Redis提供的序列化器
GenericToStringSerializer:使用Spring转换服务进行序列化
JacksonJsonRedisSerializer:使用Jackson1,将对象序列化为JSON
Jackson2JsonRedisSerializer:使用Jackson2,将对象序列化为JSON
JdkSerializationRedisSerializer:使用Java序列化
OxmSerializer:使用Spring O/X映射的编排器和解排器实现序列化,用于XML序列化
StringRedisSerializer:序列化String类型的key和value
redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Order>(Order.class));
在配置文件中追加以下代码
/** * 缓存管理器 * @param redisTemplate * @return */ @Bean public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) { RedisCacheManager cacheManager =new RedisCacheManager(redisTemplate); //设置过时时间 cacheManager.setDefaultExpiration(10); return cacheManager; }
注解 | 描述 |
---|---|
@Cacheable | 代表Spring在调用方法以前,首先应该在缓存中查找方法的返回值,若是这个值可以找到,就会返回缓存的值。不然,这个方法就会被调用,返回值会放到缓存之中 |
@CachePut | 表名Spring应该将方法的返回值放到缓存中。在方法的调用前并不会检查缓存,方法始终都会被调用 |
@CacheEvict | 代表Spring应该在缓存中清除一个或多个条目 |
@Caching | 这是一个分组的注解,可以同时应用多个其余的缓存注解 |
@Cacheable与@CachePut的一些共有属性
属性 | 类型 | 描述 |
---|---|---|
value | String[] | 要使用的缓存名称 |
condition | String | SpEL表达式,若是获得的值是false的话,不会将缓存应用到方法调用上 |
key | String | SpEL表达式,用来计算自定义的缓存key |
unless | String | SpEL表达式,若是获得的值是true的话,返回值不会放到缓存之中 |
package springmvc.orders.db; import java.util.List; import org.springframework.cache.annotation.Cacheable; import springmvc.bean.Order; public interface OrderOperations { @Cacheable("spittle") List<Order> findOrdersByType(String t); }
缓存切面会拦截调用并在缓存中查找以前以名spittle存储的返回值。缓存的key是传递到findOrdersByType()方法中的t参数。若是按照这个key可以找到值的话,就会返回找到的值,方法就不会被调用。若是没有找到值的话,那么就会调用这个方法
当在接口方法添加注解后,被注解的方法,在全部的实现继承中都会有相同的缓存规则
@CacheEvict
@CacheEvict("spittle") void remove(String Id);
@CacheEvict可以应用在返回值为void的方法上, 而@Cacheable和@CachePut须要非void的返回值,他将会做为放在缓存中的条目
属性 | 类型 | 描述 |
---|---|---|
value | String[] | 要使用的缓存名称 |
key | String | SpEL表达式,用来计算自定义的缓存key |
condition | String | SpEL表达式,若是获得的值是false的话,缓存不会应用到方法调用上 |
allEntries | boolean | 若是为true的话,特定缓存的全部条目都会被移除 |
beforeInvocation | boolean | 若是为true的话,在方法调用以前移除条目,若是为false的话,在方法成功调用以后在移除条目 |
更多内容能够关注微信公众号,或者访问AppZone网站