🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

lru-cache-api-notice

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lru-cache-api-notice

lruCache 缓存功能,重复 api 通知功能

latest
Source
npmnpm
Version
0.2.0
Version published
Weekly downloads
16
Maintainers
1
Weekly downloads
 
Created
Source

LruCacheApiNotice

一个 lruCache 缓存功能,附带 api 通知功能

安装

# npm
npm i lru-cache-api-notice
# pnpm
pnpm add lru-cache-api-notice

使用

// 在 main.js 中引入并设定参数
import { createLruCache, default as LruCache } from 'lru-cache-api-notice'
const options = {
	// 缓存时长, 单位:s
	cacheTime: 10,
	// 最大缓存数量
	maxCache: 20,
	// 启用本地存储,正式环境不推荐
	storage: localStorage || false,
	// 禁用缓存名单
	blackList: [],
	// 启用 api 通知功能
	useNotice: true
}
// 方式一
const lruCache = new LruCache(options)
// 方式二,单例
const lruCache = createLruCache(options)
// 修改参数
lruCache.setCacheTime(20)



// http.js 中引入
import { createLruCache } from 'lru-cache-api-notice'
const lruCache = createLruCache()

// request
const getCacheData = lruCache.get(key/* 根据请求接口生成的唯一 key */) ?? null
if (getCacheData !== null) {
	// 命中缓存
	return getCacheData
}

// response
lruCache.set(key, value)

lruCache

  • 使用 lruCache 后,所有请求结果均会被缓存(建议只对 get 请求做缓存处理)。如果缓存存在且在缓存有效期内,所有请求均不会发送(需自行处理)
  • 使用 useNotice 后,相同 key 的请求只会发送一次,其余请求会在第一个请求结果返回后,进行通知(失败时需自行调用 noticeReject)
// main.js

lruCache.setUseNotice(true)
function getData() {
	http.get('url')	
}
getData()
getData()
getData()
// response
if (lruCache.useNotice && responseFail) {
	lruCache.noticeReject(key)
}

License

MIT License © 2022 Owner

Keywords

lru

FAQs

Package last updated on 24 Jan 2024

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