Socket
Book a DemoInstallSign in
Socket

@braken/cache

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@braken/cache

somethings

1.1.32
latest
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

@braken/cache

Braken 框架的缓存模块,提供多级缓存支持。

安装

pnpm add @braken/cache

特性

  • 多级缓存支持
  • 缓存同步
  • 过期时间管理
  • 缓存查找
  • 缓存重写
  • 依赖注入集成

使用示例

import { Application } from '@braken/application';
import CacheServer from '@braken/cache';
import FileCache from '@braken/cache-file';
import IORedisCache from '@braken/cache-ioredis';

// 创建缓存实例
const fileCache = new FileCache();
const redisCache = new IORedisCache();

// 配置缓存服务器
CacheServer.set([fileCache, redisCache]);

// 使用缓存
@Application.Injectable
class MyService {
  @Application.Inject(CacheServer)
  private readonly cache: CacheServer;

  async getUser(id: string) {
    // 尝试从缓存读取
    const cached = await this.cache.read(`user:${id}`);
    if (cached) {
      return cached;
    }

    // 从数据库读取
    const user = await this.fetchUserFromDB(id);

    // 写入缓存
    await this.cache.write(`user:${id}`, user, Date.now() + 3600000);

    return user;
  }

  async updateUser(id: string, data: any) {
    // 更新数据库
    await this.updateUserInDB(id, data);

    // 更新缓存
    await this.cache.rewrite(`user:${id}`, data, Date.now() + 3600000, 2);
  }
}

API

CacheServer 类

主要的缓存服务器类,提供以下功能:

  • 多级缓存管理
  • 缓存同步
  • 过期时间管理
  • 缓存查找
  • 缓存重写
  • 依赖注入集成

静态方法

set

设置缓存配置:

static set(props: CacheProps[])

实例方法

write

写入缓存:

async write(key: string, value: any, expire?: number)

read

读取缓存:

async read(key: string)

has

检查缓存是否存在:

async has(key: string)

delete

删除缓存:

async delete(key: string)

expire

获取过期时间:

async expire(key: string)

find

查找缓存:

async find(key: string)

rewrite

重写缓存:

async rewrite(key: string, value: any, expire: number, i: number)

get

获取指定索引的缓存:

get<T = any>(index: number): CacheProps<T>

实现细节

  • 支持多级缓存
  • 缓存同步机制
  • 过期时间管理
  • 缓存查找策略
  • 缓存重写机制
  • 依赖注入集成

注意事项

  • 需要正确配置缓存
  • 处理缓存同步
  • 管理过期时间
  • 处理缓存失效
  • 优化缓存性能

依赖注入

@Application.Injectable
class MyService {
  @Application.Inject(CacheServer)
  private readonly cache: CacheServer;
}

许可证

MIT

FAQs

Package last updated on 09 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.