惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
雷峰网
雷峰网
Recent Announcements
Recent Announcements
月光博客
月光博客
G
Google Developers Blog
腾讯CDC
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
T
Tenable Blog
云风的 BLOG
云风的 BLOG
W
WeLiveSecurity
博客园 - 【当耐特】
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 聂微东
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
人人都是产品经理
人人都是产品经理
P
Privacy International News Feed
MyScale Blog
MyScale Blog
K
Kaspersky official blog
T
The Blog of Author Tim Ferriss
Attack and Defense Labs
Attack and Defense Labs
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
aimingoo的专栏
aimingoo的专栏
I
Intezer
Vercel News
Vercel News
小众软件
小众软件
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
P
Proofpoint News Feed
Latest news
Latest news
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tor Project blog
S
Security Affairs
P
Proofpoint News Feed
博客园 - 三生石上(FineUI控件)
博客园 - Franky
C
Cyber Attacks, Cyber Crime and Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
美团技术团队
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
Security @ Cisco Blogs
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
Project Zero
Project Zero
D
Docker
L
Lohrmann on Cybersecurity
F
Full Disclosure

Yulin Lewis' Blog

cron表达式 cmd - 常用命令 Eureka问题汇总 Excel常见用法 shell脚本常用语法 Linux常用命令 别了,向晚 必胜客自助餐 Prometheus简易入门 Chrome问题汇总 SSH命令问题汇总 PGP加解密 Apollo问题汇总 GitHub问题汇总 SmartGit问题汇总 HttpClient问题汇总 Oculus Quest2食用指南 背包英雄:匕首流无尽模式详细攻略 Linux问题汇总 Postman问题汇总 别了,珈乐 桌面窗口管理器占用内存过高 ELK系列(6) - Elasticsearch常用接口 ELK系列(5) - Elasticsearch性能调优 MyBatis问题汇总 PostgreSQL - SQL调优方案 Java - 字符编码 Java数值问题汇总 lombok问题汇总
Spring Data Redis问题汇总
雨临Lewis · 2022-01-13 · via Yulin Lewis' Blog

前言

本文基于以下版本:

1
2
3
4
5
6
<!--  对应的是3.3.0版本的jedis(redis的java客户端) -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.4.0</version>
</dependency>

SpringBoot配置Redis

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
## 配置redis连接
spring.redis.host=localhost
spring.redis.port=6379

## 是否使用redis作为cache
#spring.cache.type=none
spring.cache.type=redis

## 默认情况下redis中的数据永不过期
#spring.cache.redis.time-to-live=30s

## 是否存储null值
#spring.cache.redis.cache-null-values=false

## key是否使用前缀
#spring.cache.redis.use-key-prefix=true
#spring.cache.redis.key-prefix=tb-

RedisTemplate存储数据到Redis后key值出现\xac\xed\x00\x05

由于SpringData的redis模块用的是jedis包,因此在使用RedisTemplate操作数据时,默认使用的是JDK的序列化器JdkSerializationRedisSerializer。当存入数据到Redis后,原本的String类型或者Hash类型的key值就会变成带有\xac\xed\x00\x05前缀的值。

建议对于String、hash类型的值,使用String序列化器:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@EnableCaching
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(final RedisConnectionFactory redisConnectionFactory) {
        final RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setEnableDefaultSerializer(false);
        template.setKeySerializer(RedisSerializer.string());
        template.setValueSerializer(RedisSerializer.java());
        template.setHashKeySerializer(RedisSerializer.string());
        template.setHashValueSerializer(RedisSerializer.string());
        template.afterPropertiesSet();

        return template;
    }

}

Redis宕机导致@Cacheable直接抛出异常,服务不可用

当使用@Cacheable来将数据存入Redis时,如果Redis服务器不可达会导致系统直接抛出异常,从而使得服务不可用,正常来说就算Redis挂了也不应该影响原本的业务逻辑的正常运行下去。

需要写一个CacheErrorHandler来对上述异常进行处理:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@EnableCaching
@Configuration
@Slf4j
public class RedisConfig extends CachingConfigurerSupport {

    /*
     * (non-Javadoc)
     * @see org.springframework.cache.annotation.CachingConfigurerSupport#errorHandler()
     */
    @Override
    public CacheErrorHandler errorHandler() {

        return new CacheErrorHandler() {

            @Override
            public void handleCachePutError(final RuntimeException exception, final Cache cache, final Object key, final Object value) {
                log.error("Failed to put key '{}' into Redis Cache: {}.", key, cache.getName(), exception);
            }

            @Override
            public void handleCacheGetError(final RuntimeException exception, final Cache cache, final Object key) {
                log.error("Failed to get key '{}' from Redis Cache: {}.", key, cache.getName(), exception);
            }

            @Override
            public void handleCacheEvictError(final RuntimeException exception, final Cache cache, final Object key) {
                log.error("Failed to evict key '{}' from Redis Cache: {}.", key, cache.getName(), exception);
                throw exception;
            }

            @Override
            public void handleCacheClearError(final RuntimeException exception, final Cache cache) {
                log.error("Failed to clear Redis Cache: {}.", cache.getName(), exception);
            }
        };
    }
}

参考链接

警告

本文最后更新于 January 13, 2022,文中内容可能已过时,请谨慎使用。

赞赏支持

微信打赏 支付宝打赏