首先,我们给一个服务配置Cache,如下:
@Service('helloService')
public class HelloServiceImpl implements HelloService {
/**
* Using SpEL for conditional caching - only cache method executions when
* the name is equal to 'Joshua'
*/
@Cacheable(value='messageCache', condition=''Joshua'.equals(#name)')
public String getMessage(String name) {
System.out.println('Executing HelloServiceImpl' +
'.getHelloMessage(\'' + name + '\')');
return 'Hello ' + name + '!';
}
}
使用 Jedis 作为Redis的Java客户端,在Spring只要配置三个:
JedisConnectionFactory 有以下参数:
- hostName=”localhost”
- port=6379
- timeout=2000 ms
- database=0
- usePool=true
RedisTemplate是提供你使用连接池连接,使用JdkSerializationRedisSerializer序列化。
RedisCacheManager 是依赖