SSM整合Redis、SpringCache的实现

本文详细介绍如何在Spring Boot项目中整合Redis缓存,包括Maven依赖配置、Redis连接池和缓存管理器的设置,以及自定义缓存key生成策略。通过具体代码示例,展示了如何在service层使用@Cacheable、@CachePut和@CacheEvict注解实现数据的缓存、更新和删除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间有限,先整理实现。后面会补充详解

1.引用mvn依赖

项目中,未引用AOP 导致 

        <!--redis cache jar-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>

        <!--spring context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <!--spring aop-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>

2. 配置redis 

#redis settings
# server IP
redis.host=10.2.*.*
# server port
redis.port=6379  
# server pass
redis.pass=Jzyt2@2018
# use dbIndex
redis.database=0 
#max idel instance of jedis
redis.maxIdle=300  
redis.maxTotal=100
#if wait too long ,throw JedisConnectionException
redis.maxWait=3000  
#if true,it will validate before borrow jedis instance,what you get instance is all usefull
redis.testOnBorrow=true

3.配置 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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task"
       xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd"

       default-lazy-init="true">

    <!--
        启用注解, 扫描指定的包
    -->
    <context:component-scan base-package="com.hollysmart.admin">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <cache:annotation-driven cache-manager="redisCacheManager"/>
    <!-- redis连接池 -->
    <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.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    <!-- redis连接工厂 -->
    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <!--<property name="password" value="${redis.pass}"/>-->
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

    <!-- redis模板 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
    </bean>
<!--redis缓存管理器-->
    <bean id="redisCacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <property name="caches">
            <set>
                <!-- 这里可以配置多个redis -->
                <bean class="com.hollysmart.admin.common.cache.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate" />
                    <property name="name" value="busManuscriptService"/>
                    <!-- name对应的value要在类或方法的注解中(CacheNames={" "," "})使用 -->
                </bean>
                <bean class="com.hollysmart.admin.common.cache.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate" />
                    <property name="name" value="hotSearch"/>
                     <!-- name对应的value要在类或方法的注解中(CacheNames={" "," "})使用 -->
                </bean>
                <bean class="com.hollysmart.admin.common.cache.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate" />
                    <property name="name" value="busShuangchuangService"/>
                     <!-- name对应的value要在类或方法的注解中(CacheNames={" "," "})使用 -->
                </bean>
                <bean class="com.hollysmart.admin.common.cache.RedisCache">
                    <property name="redisTemplate" ref="redisTemplate" />
                    <property name="name" value="busShuangchuangDictService"/>
                    <!-- name对应的value要在类或方法的注解中(CacheNames={" "," "})使用 -->
                </bean>
            </set>
        </property>
    </bean>
</beans>

4. 创建缓存管理类

package com.hollysmart.admin.common.cache;

import org.apache.commons.lang3.SerializationUtils;
import org.springframework.cache.Cache;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.concurrent.Callable;

/**
 * 基于redisTemplate的缓存
 */
@Configuration
@EnableCaching
public class RedisCache implements Cache {

    private RedisTemplate<String, Object> redisTemplate; //操作redis的模板
    private String name;  // 对应redsi.xml文件中的redisCacheManager中多个redis管理器


    /**
     * 缓存清理
     */
    @Override
    public void clear() {
        redisTemplate.execute(new RedisCallback<String>() {
            @Override
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }


    /**
     * 删除缓存
     * @param key redisKey
     */
    @Override
    public void evict(Object key) {
        final String keyf = key.toString();
        redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                return connection.del(keyf.getBytes());
            }

        });

    }


    /**
     * 获取缓存
     * @param key  redisKey
     * @return obj
     */
    @Override
    public ValueWrapper get(Object key) {
        final String keyf = key.toString();
        Object object = null;
        object = redisTemplate.execute(new RedisCallback<Object>() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = keyf.getBytes();
                byte[] value = connection.get(key);
                if (value == null) {
                    return null;
                }
                return SerializationUtils.deserialize(value);
            }
        });
        ValueWrapper obj = (object != null ? new SimpleValueWrapper(object) : null);
        return obj;
    }


    /**
     * 存入数据到缓存
     * @param key redisKey
     * @param value value
     */
    @Override
    public void put(Object key, Object value) {
        final String keyString = key.toString();
        final Object valuef = value;
        final long liveTime = 86400;
        redisTemplate.execute(new RedisCallback<Long>() {
            @Override
            public Long doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] keyb = keyString.getBytes();
                byte[] valueb = SerializationUtils.serialize((Serializable) valuef);
                connection.set(keyb, valueb);
                if (liveTime > 0) {
                    connection.expire(keyb, liveTime);
                }
                return 1L;
            }
        });

    }

    @Override
    public <T> T get(Object arg0, Class<T> arg1) {
        return null;
    }

    @Override
    public <T> T get(Object key, Callable<T> valueLoader) {
        return null;
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public Object getNativeCache() {
        return this.redisTemplate;
    }

    @Override
    public ValueWrapper putIfAbsent(Object arg0, Object arg1) {
        return null;
    }

    public RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }

    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    public void setName(String name) {
        this.name = name;
    }

}

5. 使用alibaba的 fastJson将数据转JSON序列化

package com.hollysmart.admin.common.cache;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonHelper {


    /**
     * Java对象序列化为JSON字符串
     *
     * @param obj Java对象
     * @return json字符串
     */
    public static String toJson(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
    }
}

6. 判断是否为简单类型

package com.hollysmart.admin.common.cache;

import org.springframework.util.ClassUtils;

import java.net.URI;
import java.net.URL;
import java.util.Date;
import java.util.Locale;

public class BeanHelper {
    /**
     * 判断是否是简单值类型.包括:基础数据类型、CharSequence、Number、Date、URL、URI、Locale、Class;
     *
     * @param clazz
     * @return
     */
    public static boolean isSimpleValueType(Class<?> clazz) {
        return (ClassUtils.isPrimitiveOrWrapper(clazz) || clazz.isEnum() || CharSequence.class.isAssignableFrom(clazz)
                || Number.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || URI.class == clazz
                || URL.class == clazz || Locale.class == clazz || Class.class == clazz);
    }
}

7. 自定义redis缓存的key,

       1.建议在 pojo类中重写toString()方法,中间碰到一个问题,就是对参数进行拼接的时候通过hashCode()取值后,每次的hashCode的值都不一样。导致同接口同参数请求接口自定义生成的key每次都不一样,缓存失败。

       2.建议在实体类中尽量的不要循环引用,否则在序列化对象的时候,会造成 栈内存溢出异常,解决方式 在循环引用的对象get方法上添加  @JSONField(serialize = false)  来解决。

 

 

package com.hollysmart.admin.common.cache;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.Serializable;

/**
 * 自定义key生成,项目名:类名:方法名:参数名 通过sha256算法加密后生成
 */

@Configuration
public class CacheKeyGenerator implements Serializable{



    private final static int NO_PARAM_KEY = 0;
    private static final long serialVersionUID = 8350803698373991855L;
    private static String keyPrefix = "kw";// key前缀,用于区分不同项目的缓存,建议每个项目单独设置


    @Bean
    public KeyGenerator keyGenerator(){
        return (target,method,params) ->{
            char sp = ':';
            StringBuilder strBuilder = new StringBuilder(30);
            strBuilder.append(keyPrefix);
            strBuilder.append(sp);
            // 类名
            strBuilder.append(target.getClass().getSimpleName());
            strBuilder.append(sp);
            // 方法名
            strBuilder.append(method.getName());
            strBuilder.append(sp);
            if (params.length > 0) {
                // 参数值
                for (Object object : params) {
                    if (BeanHelper.isSimpleValueType(object.getClass())) {
                        strBuilder.append(object);
                    } else {
                        strBuilder.append(JsonHelper.toJson(object).hashCode());
                    }
                }
            } else {
                strBuilder.append(NO_PARAM_KEY);
            }
            return DigestUtils.sha256Hex(strBuilder.toString());
        };
    }
}

8.  在service层进行注解实现,以及 @CacheConfig @Cacheable @CachePut  @CacheEvict的 简单介绍使用

     以下是伪代码:

package com.hollysmart.admin.modules.project.service;


import com.hollysmart.admin.common.service.CrudService;
import com.hollysmart.admin.modules.project.dao.BusHotSearchDao;
import com.hollysmart.admin.modules.project.entity.BusHotSearch;
import com.hollysmart.admin.modules.project.entity.BusSearchRecord;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 热搜词 测试接口
 *
 * @CacheConfig 中  cacheNames : 定义redis缓存名称,与/spring/applicationContext.xml中的<property name="name" value="redisContent"/> 做关联
 *                   cacheManager: 定义 redis的缓存管理 与/spring/applicationContext.xml中的 <bean id="redisCacheManager" class="org.springframework.cache.support.SimpleCacheManager">做关联
 * @Cacheable 缓存查询
 * @Cacheput 缓存更新/新增 使用的方法的返回结果作为缓存内容进行缓存
 * @CacheEvict 缓存删除
 *
 * 自定义缓存主键 要求使用 keyGenerator = "keyGenerator"  || key = ""
 *
 *
 */
@Service
@Transactional(readOnly = true)
@CacheConfig(cacheNames = {"hotSearch"}, cacheManager = "redisCacheManager")
public class BusHotSearchService extends CrudService<BusHotSearchDao, BusHotSearch> {


    public List<BusHotSearch> findBusHotSearch(String searchType) {
        return dao.findBusHotSearch(searchType);
    }

    @Transactional(readOnly = false)
    public void insert(BusHotSearch busHotSearch) {
        busHotSearch.preInsert();
        dao.insert(busHotSearch);
    }

    @Transactional(readOnly = false)
    public void update(BusHotSearch busHotSearch) {
        busHotSearch.preUpdate();
        dao.update(busHotSearch);
    }

    public int findOneBusHotSearch(String searchTerm, String searchType) {
        return dao.findOneBusHotSearch(searchTerm, searchType);
    }

    @Transactional(readOnly = true)
    @Cacheable( key = "#id")
    public BusHotSearch findOneBusHotSearchById(String id) {
        logger.info("redis中没有数据,从数据库中读");
        return dao.findOneBusHotSearchById(id);
    }

    /**
     * @cache*** 底层使用的是AOP
     * @Cacheable 查询缓存,如果缓存中没有,会执行sql查询数据库
     * sync   true: 同步  false :异步
     * 
     * 
     * 
     * @cacheable()的写法
     *              @Cacheable(value = "",key = "#root.methodName + #index")
     *              @Cacheable(cacheNames = {"",""},key = "#root.methodName + #index")
     *              @Cacheable(cacheNames = {"",""},key = "#root.methodName + #index",sync = false)
     * @param index
     * @return
     */
    @Cacheable(cacheNames = {"",""},key = "#root.methodName + #index",sync = false)
    public List<BusHotSearch> findMoreBusHotSearchList(Integer index) {
        logger.info("redis中没有数据,从数据库中读");
        return dao.findMoreBusHotSearchList(index);
    }

    @Transactional(readOnly = false)
    @CachePut(key = "#busHotSearch.id")
    public BusHotSearch updateBusHotSearchList(BusHotSearch busHotSearch) {

        busHotSearch.preUpdate();
        dao.updateBusHotSearchList(busHotSearch);
        return busHotSearch;
    }

    /**
     * @CachePut() 修改缓存
     * key 缓存的key
     * keyGenerator 同key 一样,定义缓存的key,优先级比key低
     * 缓存的key 可以定义为:
     *              key = "#busHotSearch.id"        //参数id
     *              key = "#root.method"            //当前方法
     *              keyGenerator = "keyGenerator"   //自定义主键 ,上面写的自定义主键格式DigestUtils.sha256Hex(项目名:类名:方法名:参数拼接的hashCode)保证键不重复
     *              key = "#root.methodName + '_test'"          // 当前方法名+ 自定义名称  拼接
     *
     * @param busHotSearch 热搜词对象
     * @return 热搜词
     */
    @Transactional(readOnly = false)
    @CachePut(key = "#busHotSearch.id")
    public BusHotSearch insertBusHotSearchList(BusHotSearch busHotSearch) {
        busHotSearch.preInsert();
        dao.insert(busHotSearch);
        return busHotSearch;
    }

    /**
     * @CacheEvict() 删除缓存
     * 此处为定义key的值,会删除 缓存对象 为 hotSearch的所有缓存
     * allEntries 是否把上面cacheNames指定的所有的缓存都清除掉,默认false
     * beforeInvocation 是否让清理缓存动作在目标方法之前执行,默认是false,若在之后执行的话,目标方法一旦抛出异常了,那缓存就清理不掉了
     * condition SpEL,可以使用#root.**  可以搜索一下详细的表达式使用
     * @param id id
     * @return
     */
    @Transactional(readOnly = false)
//    @CacheEvict(key = "'findMoreBusHotSearchList0'",allEntries = true)
    @CacheEvict(cacheNames = {"hotSearch"},allEntries = true,beforeInvocation = false)
    public int delBusHotSearchList(String id) {

        int affectRows = dao.deleteHotSearchList(id);

        return affectRows;
    }
}

OK,有时间再对该实现做详解补充。这只是简单的使用。如果有好的建议谢谢各位留言。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值