ssm项目整合redis

1. 导入redis依赖包

<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>${spring.data.redis.version}</version>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>${redis.clients.version}</version>

</dependency>

版本

<spring.data.redis.version>1.5.0.RELEASE</spring.data.redis.version>

<redis.clients.version>2.5.0</redis.clients.version>

2. 添加redis.properties 文件

redis.host=127.0.0.1

redis.port=6379

redis.pass=

redis.database=5

redis.timeout=2000

 

redis.maxIdle=300

redis.minIdle=100

redis.maxTotal=1000

redis.testOnBorrow=true

 

引入配置文件 

    <bean id="propertyConfigurer"     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

        <property name="locations">

        <array>

        ...

<value>classpath:redis.properties</value>

</array>

</property>

    </bean> 

3. 配置spring-redis.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:tx="http://www.springframework.org/schema/tx"

       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/tx

       http://www.springframework.org/schema/tx/spring-tx.xsd

       http://www.springframework.org/schema/context

       http://www.springframework.org/schema/context/spring-context.xsd">

       

         <!-- redis -->  

       <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">

        <property name="maxIdle" value="${redis.maxIdle}" />

        <property name="maxTotal" value="${redis.maxTotal}" />

        <property name="minIdle" value="${redis.minIdle}" />

        <property name="testOnBorrow" value="${redis.testOnBorrow}" />

    </bean>

    <bean id="redisFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

          p:hostName="${redis.host}"

          p:port="${redis.port}"

          p:password="${redis.pass}"

          p:timeout="${redis.timeout}"

          p:poolConfig-ref="poolConfig"

          p:database="${redis.database}"/>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">

        <property name="connectionFactory"   ref="redisFactory" />

    </bean>

  

    <bean id="redisUtil" class="com.demo.util.RedisUtil" >  

          <property name="redisTemplate" ref="redisTemplate" />  

    </bean >  

</beans>    

web.xml中引入spring-redis.xml配置文件

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring-mybatis.xml,classpath:spring-redis.xml</param-value>

</context-param>

4. 创建redis 工具类

package com.demo.util;

import java.io.Serializable;

import java.util.Set;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.ValueOperations;

public class RedisUtil {  

    private static Logger logger = Logger.getLogger(RedisUtil.class);  

      

    private static RedisTemplate<Serializable, Object> redisTemplate;  

      

    /**

     * 写入或更新缓存

     * @param key

     * @param value

     * @return 

     */  

    public static 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("write redis is faill");  

            e.printStackTrace();  

              

        }  

        return result;  

    }  

      

      

      /**  

     * 写入缓存  

     *  设置失效时间

     * @param key  

     * @param value  

     * @return  

     */    

    public static 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) {    

            e.printStackTrace();    

        }    

        return result;    

    }    

    /**

     * 读取缓存

     * @param key

     * @return 

     */  

    public static Object get(final String key)  

    {  

        Object result = null;  

        ValueOperations<Serializable, Object> operations = redisTemplate  

                .opsForValue();  

        result = operations.get(key);  

        return result;  

    }  

      

    /**

     * 删除对应的value

     * @param key

     */  

    public static void remove(final String key)  

    {  

        if (exists(key)) {  

            redisTemplate.delete(key);  

        }  

    }  

      

    /**

     * 批量删除对应的value

     *  

     * @param keys

     */  

    public static void remove(final String... keys) {  

        for (String key : keys) {  

            remove(key);  

        }  

    }  

      

    /**

     * 批量删除key

     *  

     * @param pattern 正则表达式

     */  

    public static void removePattern(final String pattern) {  

        Set<Serializable> keys = redisTemplate.keys(pattern);  

        if (keys.size() > 0)  

            redisTemplate.delete(keys);  

    }  

      

    /**

     * 判断缓存中是否有对应的value

     *  

     * @param key

     * @return 

     */  

    public static boolean exists(final String key) {  

        return redisTemplate.hasKey(key);  

    }  

  

    public  void setRedisTemplate(  

            RedisTemplate<Serializable, Object> redisTemplate) {  

        this.redisTemplate = redisTemplate;  

    }  

}

5. 调用redis工具类的方法直接调用即可(记得先启动redis服务)

猜你喜欢

转载自blog.csdn.net/qq_21299835/article/details/79551441