zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    IDEA快捷键:查询快捷键
    IDEA快捷键:自动代码(get、set、构造方法、try...catch、if....else、重写方法、实现方法、大小写转换、格式化、方法参数提示、方法的声明)
    查找(顺序查找、折半查找、分块查找)
    Springboot:整合Mybatis
    required a bean of type 'com.zhb.dao.StudentDao' that could not be found.
    Springboot:整合Druid
    Springboot:整合JDBC
    springboot、Thymeleaf、国际化的简单使用
    Exception evaluating SpringEL expression
    java8:四大核心函数式接口(消费型、供给型、函数型、断言型)
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/14006982.html
Copyright © 2011-2022 走看看