cache-redis

缓存设计

缓存设计考虑点:

  1. 存储的数据结构
  2. 缓存容量大小
  3. 缓存有效期
  4. 容量不够时的清理策略
  5. 缓存设计相关指标(命中率、命中次数、miss率等)

缓存设计过程:

  1. 全局的hash表,Key-Value键值对
  2. 提供get/put等方法来操作缓存
  3. 规定最大容量
  4. 记录缓存访问时间,决定缓存是否失效
  5. 容量不够时LRU/FIFO/LFU来做清理
  6. 每次get方法做结果统计

guava cache

类似于ConcurrentMap,支持高并发,是线程安全的,ConcurrentMap添加元素,不显式移除的话一直存在Map中,但是Cache会有失效策略,以及支持高并发.

  • new Builder:cache构造器,builder模式
  • maximumSize:缓存最大容量
  • expireAfterAccess:缓存有效期
  • refreshAfterWrite:自动刷新缓存时间间隔
  • recordStats:记录缓存使用情况
  • CacheLoader:定义缓存刷新的方法
    例子:
 public static void test9() throws ExecutionException {
        LoadingCache<String,TestPojo> loadingCache = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterAccess(30, TimeUnit.MINUTES)
                .refreshAfterWrite(10,TimeUnit.MINUTES)
                .removalListener(new RemovalListener<String, TestPojo>() {
                    public void onRemoval(RemovalNotification<String, TestPojo> removalNotification)
                    {
                        //TODO
                    }
                })
                .recordStats()
                .build(new CacheLoader<String, TestPojo>() {
                    @Override
                    public TestPojo load(String s) throws Exception {
                        return fetchFromDB(s);
                    }
                });
        loadingCache.get("aaa"); //get时,如果缓存中没有。则到数据库中查找
        loadingCache.put("bbb",new TestPojo());
    }
    private static TestPojo fetchFromDB(String s) {
        return new TestPojo();
    }

优点:

  • 使用简单
  • 线程安全,内部实现类似于ConcurrentHashMap
  • 可以自动加载、定时更新缓存
  • 容量不够时LRU清理
  • 记录访问时间用于计算是否有效
  • key/value支持多种引用类型
  • 溶剂缓存访问数据
  • 缓存被移除或失效时可以被“监听”
  • 考虑:数据量、数据变化

局限:

  • 单个应用运行时的本地缓存,数据并没有持久化存放到某个文件或外部服务器;
  • 单机,受机器内存限制,重启应用缓存数据会丢失。应用分布式部署会出现缓存数据不一致。

redis

原理:

  • KV Nosql
  • 缓存在内存
  • 支持多种数据结构:string,hash,list,set,zset
  • 可持久化:AOF(日志) vs RDB(快照)
  • 高性能、高可靠
  • 支持主从复制
  • 支持事务

redis安装
安装参考
启动:
服务器端:src/redis-server
客户端:src/redis-cli

常用命令:
set ccc ccc
get ccc
lpush ccclist list1
lpush ccclist list2
lrange ccclist 0 -1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NestJS Cache Manager is a module for caching data in NestJS applications. It provides a simple interface for caching data using various caching strategies such as memory, Redis, and others. Redis is one of the caching strategies that NestJS Cache Manager supports. Redis is an in-memory data structure store that can be used as a database, cache, and message broker. It is often used as a cache because of its high performance and scalability. Redis stores data in memory, which makes it faster than traditional disk-based databases. Using Redis as a caching strategy in NestJS Cache Manager is easy. First, you need to install the Redis module: ``` npm install cache-manager-redis-store ``` Next, you need to configure the Redis cache in your NestJS application: ``` import { CacheModule, Module } from '@nestjs/common'; import * as redisStore from 'cache-manager-redis-store'; @Module({ imports: [ CacheModule.register({ store: redisStore, host: 'localhost', port: 6379, }), ], }) export class AppModule {} ``` In this example, we import the CacheModule and configure it to use the Redis store. We set the host and port to connect to the Redis instance. Now, you can use the cache in your NestJS application: ``` import { CacheInterceptor, CacheTTL, Controller, Get, UseInterceptors } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() @UseInterceptors(CacheInterceptor) @CacheTTL(60) findAll(): string[] { return ['Cat 1', 'Cat 2', 'Cat 3']; } } ``` In this example, we use the CacheInterceptor to cache the response of the findAll() method for 60 seconds. This means that subsequent requests for the same resource will be served from the cache, which improves performance and reduces the load on the server. Overall, using Redis as a caching strategy in NestJS Cache Manager is a great way to improve the performance and scalability of your NestJS application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值