spring boot 集成 redis
spring boot 集成redis,本人选用xml文件的形式,由于spring boot 得简单易用,网上多用注解的形式集成redis
,话不多说,上代码
1、首先配置文件redis-config.properties,初始化redis的一些参数,包括端口、密码等
redis.maxIdle=10
redis.maxTotal=100
redis.database=2
redis.timeout=3000
redis.host=127.0.0.1
redis.password=
redis.port=6379
2、xml配置文件redis-config.xml
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxTotal}"/>
<property name="maxIdle" value="${redis.maxIdle}"/>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="database" value="${redis.database}"/>
<property name="timeout" value="${redis.timeout}"/>
<property name="usePool" value="true"/>
<property name="poolConfig" value="jedisPoolConfig"/>
<property name="port" value="${redis.port}"/>
<property name="password" value="${redis.password}"/>
<property name="hostName" value="${redis.host}"/>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory"/>
<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="enableTransactionSupport" value="false"/>
</bean>
<!-- <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate"></constructor-arg>
缓存失效时间,这里被我注释了,这里可以设置单个存放于缓存中的参数的有效时间,当然我的失效时间存放于java代码中
<property name="expires">
<map>
<entry key="" value=""></entry>
<entry key="" value=""></entry>
</map>
</property>
</bean> -->
<bean id="redisUtil" class="com.wzy.redis.RedisUtil">
<property name="redisTemplate" ref="redisTemplate" />
</bean>
3、RedisUtil工具类
@Component
public class RedisUtil {
private static final Logger logger = LoggerFactory.getLogger(RedisUtil.class);
private RedisTemplate<Serializable, Object> redisTemplate;
/**
* 批量删除对应的key
* @param keys
*/
public void remove(final String... keys) {
for(String key : keys) {
remove(key);
}
}
/**
* 删除缓存中相应额key
* @param key
*/
public void remove(String key) {
if(exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断是否存在相应的key
* @param key
* @return
*/
public boolean exists(String key) {
return redisTemplate.hasKey(key);
}
/**
* 批量删除key
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if(keys.size() > 0) {
redisTemplate.delete(keys);
}
}
/**
* 读取缓存
* @param key
* @return
*/
public Object get(final String key) {
Object result = null;
ValueOperations< Serializable, Object> operations = redisTemplate.opsForValue();
result = operations.get(key);
return result;
}
/**
* 写入缓存
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
result = true;
}catch (Exception e) {
logger.error("set cache error", e);
}
return result;
}
/**
* 写入缓存时间,单位秒
* @param key
* @param value
* @param expireTime
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
}catch (Exception e) {
logger.error("set cache error", e);
}
return result;
}
/**
* 以增量的方式将long值存储在变量中。
* @param key
* @param delta
* @return
*/
public long increment(final String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
public void setRedisTemplate(RedisTemplate<Serializable, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
这样spring boot就集成了redis,打开redis服务器,就可以使用了。
推荐阅读
-
解决spring boot1.5以上版本@ConfigurationProperties提示“Spring Boot Configuration Annotation Processor not.."
-
什么是Spring Boot
-
Spring集成jedis的配置与使用简单实例
-
Spring-Boot 集成Solr客户端的详细步骤
-
Spring Boot实现图片上传功能
-
详解Spring Boot 使用Spring security 集成CAS
-
用Spring Boot进行后端开发(二):与微信小程序的交互,在微信小程序端获取数据并显示
-
Spring Boot 发送邮件功能案例分析
-
详解配置spring-boot-actuator时候遇到的一些小问题
-
详解Spring Boot配置排序依赖技巧