redis的使用
来源:互联网 发布:js秒表倒计时 编辑:程序博客网 时间:2024/06/15 18:22
使用redis首先需要配置redis
@Configuration@EnableAutoConfiguration@EnableCachingpublic class RedisCacheConfig { @Value("${redis.host}") private String host; @Value("${redis.port}") private int port; @Value("${redis.password}") private String password; @Value("${redis.pool.maxTotal}") private int maxTotal; @Autowired @Qualifier("redisTemplate") private RedisTemplate<Object, Object> redisTemplate; // redis缓存失效时间, 24小时 private Integer cacheExpirationTime = 60 * 60; @Bean public JedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) { JedisConnectionFactory factory = new JedisConnectionFactory(); factory.setHostName(host); factory.setPort(port); if(StringUtils.isNotEmpty(password)){ factory.setPassword(password); } factory.setPoolConfig(jedisPoolConfig); return factory; } @Bean public JedisPoolConfig jedisPoolConfig() { JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setBlockWhenExhausted(true); jedisPoolConfig.setTestOnBorrow(true); jedisPoolConfig.setMaxTotal(maxTotal); return jedisPoolConfig; } @Bean public CacheClient redisClientFactory() { CacheClient cc = new BaseRedisCacheImpl(); return cc; } @Bean(name = "redisTemplate") public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) { GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(); RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); template.setKeySerializer(new StringRedisSerializer()); // 对于普通K-V操作时,key采取的序列化策略 template.setValueSerializer(serializer); // value采取的序列化策略 template.setHashKeySerializer(serializer); // 在hash数据结构中,hash-key的序列化策略 template.setHashValueSerializer(serializer); // 在hash数据结构中,hash-key的序列化策略 template.setConnectionFactory(factory); template.afterPropertiesSet(); return template; } @Bean public CacheManagerCustomizer<RedisCacheManager> cacheManagerCustomizer() { return new CacheManagerCustomizer<RedisCacheManager>() { @Override public void customize(RedisCacheManager cacheManager) { cacheManager.setDefaultExpiration(cacheExpirationTime); Map<String, Long> expires = Maps.newHashMap(); expires.put(CacheModule.GAME, 2*3600l); expires.put(CacheModule.APP, 24*3600l); expires.put(CacheModule.MIN5, 5*60l); expires.put(CacheModule.HOUR1, 3600l); expires.put(CacheModule.CERT, 24*3600l); cacheManager.setExpires(expires); cacheManager.setCacheNames( Arrays.asList(CacheModule.COMMENT, CacheModule.GAME, CacheModule.USER, CacheModule.WEB, CacheModule.APP, CacheModule.SHARE, CacheModule.CERT, CacheModule.HOUR1, CacheModule.MIN5)); } }; }}
Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
从3.1开始,Spring引入了对Cache的支持。其使用方法和原理都类似于Spring对事务管理的支持。Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回。所以在使用Spring Cache的时候我们要保证我们缓存的方法对于相同的方法参数要有相同的返回结果。
使用Spring Cache需要我们做两方面的事:
n 声明某些方法使用缓存
n 配置Spring对Cache的支持
和Spring对事务管理的支持一样,Spring对Cache的支持也有基于注解和基于XML配置两种方式。下面我们先来看看基于注解的方式。
1 基于注解的支持
Spring为我们提供了几个注解来支持Spring Cache。其核心主要是@Cacheable和@CacheEvict。使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回结果,而使用@CacheEvict标记的方法会在方法执行前或者执行后移除Spring Cache中的某些元素。下面我们将来详细介绍一下Spring基于注解对Cache的支持所提供的几个注解。
1.1 @Cacheable
@Cacheable可以标记在一个方法上,也可以标记在一个类上。当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果,至于键的话,Spring又支持两种策略,默认策略和自定义策略,这个稍后会进行说明。需要注意的是当一个支持缓存的方法在对象内部被调用时是不会触发缓存功能的。@Cacheable可以指定三个属性,value、key和condition。
1.2 @CachePut
在支持Spring Cache的环境下,对于使用@Cacheable标注的方法,Spring在每次执行前都会检查Cache中是否存在相同key的缓存元素,如果存在就不再执行该方法,而是直接从缓存中获取结果进行返回,否则才会执行并将返回结果存入指定的缓存中。@CachePut也可以声明一个方法支持缓存功能。与@Cacheable不同的是使用@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。 @CachePut也可以标注在类上和方法上。使用@CachePut时我们可以指定的属性跟@Cacheable是一样的。
@CachePut(“users”)//每次都会执行方法,并将结果存入指定的缓存中
public User find(Integer id) {
returnnull;
}
1.3 @CacheEvict
@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。其中value、key和condition的语义与@Cacheable对应的属性类似。即value表示清除操作是发生在哪些Cache上的(对应Cache的名称);key表示需要清除的是哪个key,如未指定则会使用默认策略生成的key;condition表示清除操作发生的条件。下面我们来介绍一下新出现的两个属性allEntries和beforeInvocation。
1.4 @Caching
@Caching注解可以让我们在一个方法或者类上同时指定多个Spring Cache相关的注解。其拥有三个属性:cacheable、put和evict,分别用于指定@Cacheable、@CachePut和@CacheEvict。
@Caching(cacheable = @Cacheable(“users”), evict = { @CacheEvict(“cache2”),
@CacheEvict(value = "cache3", allEntries = true) })
public User find(Integer id) {
returnnull;
}
重点(例子)
@CacheEvict 应该和 @Cacheable 搭配使用
@CacheEvict(cacheNames = CacheModule.APP, key = "T(com.gws.base.CachePrefix).APP.concat(T(String).valueOf(#app.getAppId()))")@Override public App updateApp(App app) { if (null == app){ return null; } appMaster.updateById(app,app.getAppId(),"appId","appName","appVersion","osType","icon1","icon2","pkgPath","appAbstract"); return app; }
@Cacheable(cacheNames = CacheModule.APP, key = "T(com.gws.base.CachePrefix).APP.concat(T(String).valueOf(#appId))") @Override public App getApp(Long appId) { if (null == appId) { return null; } return appSlave.findOne(appId); }
执行流程:
1.当执行获取的方法的时候,会将缓存放到数据库
2.当执行更改的时候,会清理掉数据库的缓存
结合业务学技术。
- redis--2--redis的使用
- redis的内存使用
- Redis的使用
- Redis 的使用
- php redis的使用
- redis pipeline的使用
- redis的使用
- redis 的使用
- redis的基本使用
- redis的使用
- Redis 的使用
- Redis浅显的使用
- redis的使用
- Redis的使用
- redis spring的使用
- mybatis-redis的使用
- Redis的使用
- redis的简单使用
- excel操作 System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x800AC472
- HUD-2046(递推)
- (Java/.net)字符串在一个字符串中出现的次数
- Android手机如何查看log日志
- 看透 Spring MVC 源代码分析与实践 —— Spring MVC 组件分析
- redis的使用
- tomcat 启动 maven 报错 org.apache.catalina.LifecycleException
- 贪心
- Gson的fromJson()方法
- 单元测试
- Zookeeper-3.4.9 集群搭建
- Linux errno 错误对照表
- springmvc接收前台(可以是ajax)传来的数组list,map,set等集合,复杂对象集合等图文详解
- 我所理解的RESTful Web API [设计篇]