谷粒商城缓存(二十三)

 151、缓存-缓存使用-本地缓存与分布式缓存 - 157、缓存-缓存使用-本地锁在分布式下的问题

 主要就是把数据缓存到redis中

@Override
    //会内存异常,主要就是lettuce的bug,导致内存溢出,它是使用netty进行网络通信
    //luttuce的bug导致netty堆外内存溢出 -Xmx300m;netty如没有指定,默认是-Xmx300m
    //不能使用-Dio.netty.maxDirectMemory只去调大堆外内存
    //1.升级Luttuce客户端, 2切换jedis
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        String catalogJSON = stringRedisTemplate.opsForValue().get("catalogJSON");
        if(StringUtils.isBlank("catalogJSON") || catalogJSON == null){
            Map<String, List<Catalog2Vo>> catalogJsonFromDb = getCatalogJsonFromDb();
            String s = JSON.toJSONString(catalogJsonFromDb);
            stringRedisTemplate.opsForValue().set("catalogJSON",s,1,TimeUnit.DAYS);
            return catalogJsonFromDb;
        }
        Map<String, List<Catalog2Vo>> map = JSON.parseObject(catalogJSON, new TypeReference<Map<String, List<Catalog2Vo>>>() {
        });
        return map;
    }

但是当压测到一定程度的时候,会报内存溢出

 修改方式如下:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

然后就是redis几个常见的问题 

 可以参考这篇博客:https://www.cnblogs.com/dalianpai/p/12678795.html

谷粒商城推荐的方案如下:

1.空结果缓存:解决缓存穿透

2.设置过期时间(加随机值):解决缓存雪崩

3:加锁解决缓存击穿

 

 

 

当项目是分布式项目的时候,不适合用本地锁,要改成分布式锁,我之前研究的是redis+lua的方式

具体可以查看之前写的博客

https://www.cnblogs.com/dalianpai/p/12714132.html

原文地址:https://www.cnblogs.com/dalianpai/p/13177995.html