二十三、Spring Boot自定义Redis实现缓存机制

来源:互联网 发布:搜索整理15个淘宝网店 编辑:程序博客网 时间:2024/05/22 02:00

基于spring-date的已经实现了 CacheManager缓存管理器和caceh等,具体使用参考:http://blog.csdn.net/l_sail/article/details/70306339

工程结构

这里写图片描述

maven依赖

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>com.lf</groupId>    <artifactId>cache-RedisTemplate</artifactId>    <version>0.0.1-SNAPSHOT</version>    <packaging>jar</packaging>    <name>cache-RedisTemplate</name>    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.2.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-data-redis</artifactId>        </dependency>        <!--添加spring对cache的支持-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-cache</artifactId>        </dependency>        <dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId>mybatis-spring-boot-starter</artifactId>            <version>1.3.0</version>        </dependency>        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <scope>runtime</scope>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

自定义缓存管理

package com.lf.redisConfig;import com.lf.config.RedisObjectSerializer;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;/** * Created by LF on 2017/4/20. */@Configurationpublic class RedisCacheConfig extends CachingConfigurerSupport {    @Bean    public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {        CacheManager cacheManager = new RedisCacheManager(redisTemplate);        return cacheManager;    }    @Bean    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {        RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();        redisTemplate.setConnectionFactory(factory);        //key序列化方式;(不然会出现乱码;)        //如果实现了key的自定义方式 方法上有Long等非String类型的话,会报类型转换错误;        //则要实现RedisSerializer的序列化,        redisTemplate.setKeySerializer(new RedisObjectSerializer());        redisTemplate.setValueSerializer(new RedisObjectSerializer());        return redisTemplate;    }    //自定义key的实现方式    @Override    public KeyGenerator keyGenerator() {        System.out.println("RedisCacheConfig.keyGenerator()");        return (o, method, objects) -> {            StringBuilder sb = new StringBuilder();            sb.append(o.getClass().getName());            sb.append(method.getName());            for (Object obj : objects) {                sb.append(obj.toString());            }            System.out.println("keyGenerator=" + sb.toString());            return sb.toString();        };    }}

其他文件参看:http://blog.csdn.net/l_sail/article/details/70306339

0 0
原创粉丝点击