欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot 集成 redis

程序员文章站 2022-06-08 21:47:41
...

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 boot 集成 redis
            
    
    博客分类: spring boot 集成 redis spring bootjavaredis