搜索
首页Javajava教程Java中关于SSM之整合Redis的详解

Java中关于SSM之整合Redis的详解

Sep 30, 2017 am 10:31 AM
javaredis详解

Java中关于SSM之整合Redis的详解

Redis安装与使用

第一步当然是安装Redis,这里以Windows上的安装为例。

  • 首先下载Redis,可以选择msi或zip包安装方式

  • zip方式需打开cmd窗口,在解压后的目录下运行redis-server redis.windows.conf启动Redis

  • 采用msi方式安装后Redis默认启动,不需要进行任何配置

  • 可以在redis.windows.conf文件中修改Redis端口号、密码等配置,修改完成后使用redis-server redis.windows.conf命令重新启动

  • 在Redis安装目录下执行redis-cli -h 127.0.0.1 -p 6379 -a 密码打开Redis操作界面

  • 如果报错(error) ERR operation not permitted,使用auth 密码进行验证

SSM整合Redis

这里直接在上一篇SSM之框架整合的基础上进行Redis整合,这里需要注意,存入Redis的pojo类必须实现Serializable接口

配置pom.xml引入Redis依赖


<!--redis--><dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.6.1.RELEASE</version></dependency><dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.3</version></dependency>

redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.password=redis
redis.maxIdle=100
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=100000
defaultCacheExpireTime=3600

applicationContext-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:mvc="http://www.springframework.org/schema/mvc"       
xmlns:context="http://www.springframework.org/schema/context"       
xmlns:aop="http://www.springframework.org/schema/aop"       
xmlns:tx="http://www.springframework.org/schema/tx"       
xsi:schemaLocation="http://www.springframework.org/schema/beans        
http://www.springframework.org/schema/beans/spring-beans.xsd        
http://www.springframework.org/schema/mvc        
http://www.springframework.org/schema/mvc/spring-mvc.xsd        
http://www.springframework.org/schema/context        
http://www.springframework.org/schema/context/spring-context.xsd        
http://www.springframework.org/schema/aop        
http://www.springframework.org/schema/aop/spring-aop.xsd        
http://www.springframework.org/schema/tx        
http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--引入Redis配置文件-->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>

    <!-- jedis 连接池配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>
    <!-- redis连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig"/>
        <property name="port" value="${redis.port}"/>
        <property name="hostName" value="${redis.host}"/>
        <property name="password" value="${redis.password}"/>
        <property name="timeout" value="${redis.timeout}"></property>
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>
    <!-- 缓存拦截器配置 -->
    <bean id="methodCacheInterceptor" class="com.zkh.interceptor.MethodCacheInterceptor">
        <property name="redisUtil" ref="redisUtil"/>
        <property name="defaultCacheExpireTime" value="${defaultCacheExpireTime}"/>
        <!-- 禁用缓存的类名列表 -->
        <property name="targetNamesList">
            <list>
                <value></value>
            </list>
        </property>
        <!-- 禁用缓存的方法名列表 -->
        <property name="methodNamesList">
            <list>
                <value></value>
            </list>
        </property>
    </bean>
    <bean id="redisUtil" class="com.zkh.util.RedisUtil">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
    <!--配置切面拦截方法 -->
    <aop:config proxy-target-class="true">
        <aop:pointcut id="controllerMethodPointcut" expression="        
        execution(* com.zkh.service.impl.*.select*(..))"/>
        <aop:advisor advice-ref="methodCacheInterceptor" pointcut-ref="controllerMethodPointcut"/>
    </aop:config></beans>

MethodCacheInterceptor.java


package com.zkh.interceptor;
import com.zkh.util.RedisUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.util.List;
/** 
* Redis缓存过滤器 
*/
public class MethodCacheInterceptor implements MethodInterceptor {    
private RedisUtil redisUtil;    
private List<String> targetNamesList; // 禁用缓存的类名列表
    private List<String> methodNamesList; // 禁用缓存的方法列表
    private String defaultCacheExpireTime; // 缓存默认的过期时间

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        Object value = null;

        String targetName = invocation.getThis().getClass().getName();
        String methodName = invocation.getMethod().getName();        
        if (!isAddCache(targetName, methodName)) {            
        // 跳过缓存返回结果
            return invocation.proceed();
        }
        Object[] arguments = invocation.getArguments();
        String key = getCacheKey(targetName, methodName, arguments);        
        try {            // 判断是否有缓存
            if (redisUtil.exists(key)) {                
            return redisUtil.get(key);
            }            // 写入缓存
            value = invocation.proceed();            
            if (value != null) {                
            final String tkey = key;                
            final Object tvalue = value;                
            new Thread(new Runnable() {                    
            @Override
                    public void run() {
                        redisUtil.set(tkey, tvalue, Long.parseLong(defaultCacheExpireTime));
                    }
                }).start();
            }
        } catch (Exception e) {
            e.printStackTrace();            
            if (value == null) {                
            return invocation.proceed();
            }
        }        return value;
    }    
    /**     
    * 是否加入缓存     
    *     
    * @return     
    */
    private boolean isAddCache(String targetName, String methodName) {        
    boolean flag = true;        
    if (targetNamesList.contains(targetName)
                || methodNamesList.contains(methodName) || targetName.contains("$$EnhancerBySpringCGLIB$$")) {
            flag = false;
        }        return flag;
    }    
    /**     
    
    * 创建缓存key     
    *     
    * @param targetName     
    
   * @param methodName     
   * @param arguments    
    */
    private String getCacheKey(String targetName, String methodName,
                               Object[] arguments) {
        StringBuffer sbu = new StringBuffer();
        sbu.append(targetName).append("_").append(methodName);        
        
        if ((arguments != null) && (arguments.length != 0)) {           
         for (int i = 0; i < arguments.length; i++) {
                sbu.append("_").append(arguments[i]);
            }
        }        return sbu.toString();
    }    public void setRedisUtil(RedisUtil redisUtil) {        
    this.redisUtil = redisUtil;
    }    public void setTargetNamesList(List<String> targetNamesList) {        
    this.targetNamesList = targetNamesList;
    }    public void setMethodNamesList(List<String> methodNamesList) {        
    this.methodNamesList = methodNamesList;
    }    public void setDefaultCacheExpireTime(String defaultCacheExpireTime) {        
    
    this.defaultCacheExpireTime = defaultCacheExpireTime;
    }
}

RedisUtil.java 工具类

package com.zkh.util;import org.apache.log4j.Logger;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.ValueOperations;import java.io.Serializable;import java.util.Set;import java.util.concurrent.TimeUnit;/** * Redis工具类 */public class RedisUtil {    private RedisTemplate<Serializable, Object> redisTemplate;    /**     * 批量删除对应的value     *     * @param keys     */
    public void remove(final String... keys) {        for (String key : keys) {            remove(key);
        }
    }    /**     * 批量删除key     *     * @param pattern     */
    public void removePattern(final String pattern) {
        Set<Serializable> keys = redisTemplate.keys(pattern);        if (keys.size() > 0)
            redisTemplate.delete(keys);
    }    /**     * 删除对应的value     *     * @param key     */
    public void remove(final String key) {        if (exists(key)) {
            redisTemplate.delete(key);
        }
    }    /**     * 判断缓存中是否有对应的value     *     * @param key     * @return     */
    public boolean exists(final String key) {        return redisTemplate.hasKey(key);
    }    /**     * 读取缓存     *     * @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) {
            e.printStackTrace();
        }        return result;
    }    /**     * 写入缓存     *     * @param key     * @param value     * @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) {
            e.printStackTrace();
        }        return result;
    }    public void setRedisTemplate(
            RedisTemplate<Serializable, Object> redisTemplate) {        this.redisTemplate = redisTemplate;
    }
}


效果展示

Java中关于SSM之整合Redis的详解

刚开始Redis中没有任何记录,接下来访问一下第一页记录

Java中关于SSM之整合Redis的详解

再查看缓存,记录已经存如Redis,并且第一次访问会从Mysql中读取数据

Java中关于SSM之整合Redis的详解

Java中关于SSM之整合Redis的详解

F5刷新页面,从Tomcat控制台可以看到没有进行SQL查询,而是直接从Redis中读取缓存数据,减轻了数据库的负担

Java中关于SSM之整合Redis的详解

Java中关于SSM之整合Redis的详解

以上是Java中关于SSM之整合Redis的详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Spring Boot子线程如何访问主线程的请求信息?Spring Boot子线程如何访问主线程的请求信息?Apr 19, 2025 pm 06:03 PM

SpringBoot子线程无法访问主线程Request信息解决方案在Spring...

Java单线程下的指令重排序会影响System.out.println的输出顺序吗?Java单线程下的指令重排序会影响System.out.println的输出顺序吗?Apr 19, 2025 pm 06:00 PM

Java单线程下的指令重排序与输出顺序在Java编程中,指令重排序是一个常见的优化技术,用于提高程序的执行效�...

IntelliJ IDEA是如何通过JavaAgent技术识别Spring Boot项目的端口号的?IntelliJ IDEA是如何通过JavaAgent技术识别Spring Boot项目的端口号的?Apr 19, 2025 pm 05:57 PM

IntelliJIDEA如何识别SpringBoot项目的端口号?在使用IntelliJIDEAUltimate版本时,启动Spring...

如何通过 OAuth2.0 的 scope 机制精细控制 access_token 的接口访问权限?如何通过 OAuth2.0 的 scope 机制精细控制 access_token 的接口访问权限?Apr 19, 2025 pm 05:54 PM

通过OAuth2.0的access_token如何精细控制接口访问权限?在现代应用开发中,OAuth2.0...

RuoYi框架如何实现Bean依赖注入而无需显式编写DataSource实现类?RuoYi框架如何实现Bean依赖注入而无需显式编写DataSource实现类?Apr 19, 2025 pm 05:51 PM

深入剖析RuoYi框架的Bean依赖注入机制:无需显式实现类RuoYi框架是一个流行的Java前后端分离框架,其简洁的代码...

使用RedisTemplate进行批量查询时返回值为空的原因是什么?如何解决这个问题?使用RedisTemplate进行批量查询时返回值为空的原因是什么?如何解决这个问题?Apr 19, 2025 pm 05:48 PM

使用RedisTemplate进行批量查询时返回值为空的原因及解决方案在使用SpringData...

在Java中如何在一个Map中使用不同类型的Key?在Java中如何在一个Map中使用不同类型的Key?Apr 19, 2025 pm 05:45 PM

在Java中如何在同一个Map中使用不同类型的Key在Java编程中,我们经常会使用Map数据结构来存储键值对。然而,有�...

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用