SSM项目集成Redis

1. 加入依赖

<!--redis-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
    <!-- config redis data and client jar-->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.0.RELEASE</version>
    </dependency>

2. 配置文件redis-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描redis配置文件-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:redis.properties"/>
    <!--设置连接池-->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}" />
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>
    <!--设置链接属性-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.host}"
          p:port="${redis.port}"
          p:password=""
          p:pool-config-ref="poolConfig"
          p:timeout="100000"/>
    <!-- Jedis模板配置  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory"   ref="connectionFactory" />
    </bean>
 
</beans>

redis.properties

redis.host=192.168.181.130 
redis.port=6379
redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true

在 Spring 的配置文件中引入 redis 的 xml 文件

<import resurce="classpath:redis-context.xml"/>

3. 测试

service:

@Service
public class RedisServiceImpl implements RedisService {
 
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    /*
    * 将字符串类型的键值对保存到Redis时调用
    * */
    @Override
    public Boolean saveNormalStringKeyValue(String normalKey, String normalValue, Integer timeoutMinute) {
        // 获取字符串操作器对象
        ValueOperations<String, String> operator = redisTemplate.opsForValue();
        // 判断timeoutMinute值:是否为无效值
        if(timeoutMinute == null || timeoutMinute == 0) {
            return false;
        }
        // 判断timeoutMinute值:是否为不设置过期时间
        if(timeoutMinute == -1) {
            // 按照不设置过期时间的方式执行保存
            try {
                operator.set(normalKey, normalValue);
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            // 返回结果
            return true;
        }
        // 按照设置过期时间的方式执行保存
        try {
            operator.set(normalKey, normalValue, timeoutMinute, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
 
    /**
     * 根据key查询对应value时调用
     */
    @Override
    public String retrieveStringValueByStringKey(String normalKey) {
 
        try {
            String value = redisTemplate.opsForValue().get(normalKey);
 
            return value;
 
        } catch (Exception e) {
            e.printStackTrace();
 
            return "";
        }
    }
 
    /**
     * 根据key删除对应value时调用
     */
    @Override
    public Boolean removeByKey(String key) {
 
        try {
            redisTemplate.delete(key);
 
            return true;
 
        } catch (Exception e) {
            e.printStackTrace();
 
            return false;
        }
    }
}

在 controller 中调用 service :

redisservice.xxx();

原文地址:https://www.cnblogs.com/zhouheblog/p/14006982.html