大型网站的缓存设计Guava+redis实现多级缓存

应用场景

当缓存服务器重启或者大量缓存集中在某一个时间段失效,这样在失效的时候,也会给后端系统带来很大压力。突然间大量的key失效了或redis重启,大量访问数据库,数据库崩溃,这时候就需要设置一个本地缓存作为二级缓存来解决这个问题。

本地缓存的应用场景:

  • 对性能有非常高的要求
  • 不经常变化
  • 占用内存不大
  • 有访问整个集合的需求
  • 数据允许不时时一致

实现原理

数据从服务层读取,然后放到本地缓存中(Guava),如果出现超时或读取为空,则返回原来本地缓存的数据。这样如果出现分布式缓存redis挂掉等情况,依然可以从本地缓存中获取到数据,不会出现项目的瘫痪。

在这里插入图片描述

代码逻辑

引入maven坐标

	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.2</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-data-redis</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.2-jre</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

具体逻辑实现部分,先从本地缓存Guava Cache中获取要查询的数据,如果没有则从redis中查询,如果redis 中也没有,则去mysql中查询,最后回写到redis,再回写到本地缓存Guava Cache中返回查询结果

@Service
public class PositionServiceImpl implements PositionService {

    @Autowired
    PositionMapper positionMapper;

    @Autowired
    RedisTemplate redisTemplate;

    private static Cache<Object, Object> cache = CacheBuilder.newBuilder().expireAfterWrite(5,TimeUnit.SECONDS).build();

    @Override
    public List<Position> getHotPosition() throws ExecutionException {
		//从guava本地缓存中获取数据,如果没有则从redis中回源
        Object value = cache.get("position", new Callable() {
            @Override
            public Object call() throws Exception {

                return getHotPositionListFromRedis();
            }
        });

        if(value != null){
            return (List<Position>)value;
        }

        return null;
    }

    @Override
    public List<Position> getHotPositionListFromRedis()  {

        Object position = redisTemplate.opsForValue().get("position");
        System.out.println("从redis中获取数据");

        if(position == null) {
            //从mysql中获取数据
            List<Position> positionList = positionMapper.select(null);
            System.out.println("从mysql中获取数据");

            //同步至redis
            redisTemplate.opsForValue().set("position",positionList);
            System.out.println("同步至redis");

            redisTemplate.expire("position",5, TimeUnit.SECONDS);

            return positionList;
        }

        return (List<Position>)position;
    }

}

调用查询接口

@RestController
@RequestMapping("position")
public class PositionController {

    @Autowired
    PositionService positionService;

    @GetMapping
    public List<Position> getHotPosition() throws ExecutionException {

        List<Position> result = positionService.getHotPosition();

        return result;
    }

}

实体类

@Table(name = "t_position")
public class Position implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

Mapper

@org.apache.ibatis.annotations.Mapper
public interface PositionMapper extends Mapper<Position> {
}

配置文件信息

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lg?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=docker

logging.level.com.cache.cachework.dao.mapper=debug

spring.redis.cluster.nodes=127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005,127.0.0.1:7006
spring.redis.database=0
spring.redis.timeout=5000

效果演示

首次请求,回源操作从mysql->redis->cache
在这里插入图片描述

在这里插入图片描述
然后把redis停掉,模拟宕机,继续请求
从本地缓存Guava Cache中获取到查询结果
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
二级缓存是指在应用程序中同时使用两种不同的缓存技术,通常是将本地缓存和分布式缓存结合使用,以提高缓存的效率和可靠性。GuavaRedis都是常用的缓存库,下面介绍它们如何实现二级缓存。 1. Guava实现二级缓存 Guava是一个开源的Java工具库,其中包含了许多常用的工具类和数据结构,包括本地缓存Guava本地缓存是指将数据存储在应用程序内存中的缓存,可以用于提高应用程序的性能和响应速度。但是,本地缓存的生命周期受到应用程序的生命周期限制,一旦应用程序结束,缓存中的数据也就不存在了。为了解决这个问题,我们可以将Guava本地缓存和分布式缓存结合使用,实现二级缓存。 具体实现方法如下: 1)创建Guava本地缓存 ```java LoadingCache<String, Object> localCache = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(new CacheLoader<String, Object>() { @Override public Object load(String key) throws Exception { // 从数据库或其他数据源中加载数据 return loadDataFromDatabase(key); } }); ``` 2)创建Redis分布式缓存 ```java JedisPool jedisPool = new JedisPool("localhost", 6379); Jedis jedis = jedisPool.getResource(); ``` 3)在应用程序中使用二级缓存 ```java public Object getObject(String key) { Object value = null; try { // 先从本地缓存中获取数据 value = localCache.get(key); } catch (Exception e) { e.printStackTrace(); } if (value == null) { // 如果本地缓存中没有数据,则从Redis缓存中获取数据 byte[] bytes = jedis.get(key.getBytes()); if (bytes != null) { value = SerializationUtils.deserialize(bytes); // 将数据存储到本地缓存中 localCache.put(key, value); } } if (value == null) { // 如果Redis缓存中也没有数据,则从数据库或其他数据源中加载数据 value = loadDataFromDatabase(key); // 将数据存储到Redis缓存和本地缓存中 byte[] bytes = SerializationUtils.serialize(value); jedis.set(key.getBytes(), bytes); localCache.put(key, value); } return value; } ``` 2. Redis实现二级缓存 Redis是一个开源的内存数据库,可以用于存储和管理缓存数据。Redis分布式缓存的优点是可以存储大量的数据,并且可以跨多个应用程序共享数据,但是它的缺点是需要额外的硬件和网络资源来支持,同时也存在单点故障的风险。为了解决这个问题,我们可以将Redis缓存和本地缓存结合使用,实现二级缓存。 具体实现方法如下: 1)创建Redis缓存客户端 ```java JedisPool jedisPool = new JedisPool("localhost", 6379); Jedis jedis = jedisPool.getResource(); ``` 2)创建Guava本地缓存 ```java LoadingCache<String, Object> localCache = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(10, TimeUnit.MINUTES) .build(new CacheLoader<String, Object>() { @Override public Object load(String key) throws Exception { // 从Redis缓存中加载数据 byte[] bytes = jedis.get(key.getBytes()); if (bytes != null) { return SerializationUtils.deserialize(bytes); } // 如果Redis缓存中没有数据,则从数据库或其他数据源中加载数据 return loadDataFromDatabase(key); } }); ``` 3)在应用程序中使用二级缓存 ```java public Object getObject(String key) { Object value = null; try { // 先从本地缓存中获取数据 value = localCache.get(key); } catch (Exception e) { e.printStackTrace(); } if (value == null) { // 如果本地缓存中没有数据,则从Redis缓存中获取数据 byte[] bytes = jedis.get(key.getBytes()); if (bytes != null) { value = SerializationUtils.deserialize(bytes); // 将数据存储到本地缓存中 localCache.put(key, value); } } if (value == null) { // 如果Redis缓存中也没有数据,则从数据库或其他数据源中加载数据 value = loadDataFromDatabase(key); // 将数据存储到Redis缓存和本地缓存中 byte[] bytes = SerializationUtils.serialize(value); jedis.set(key.getBytes(), bytes); localCache.put(key, value); } return value; } ``` 以上就是GuavaRedis实现二级缓存的方法。需要注意的是,二级缓存实现需要综合考虑应用程序的性能、复杂度、可靠性和安全性等方面的因素,选择合适的缓存技术和策略,才能达到最优的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值