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網站