npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

frontend-cache-manager

v1.0.3

Published

CacheManager 是一个用于管理前端缓存的 JavaScript 类。它提供了一系列方法来存储、检索和清除缓存数据,支持使用 `localStorage` 或 `sessionStorage` 作为存储介质,并实现了简单的 LRU (Least Recently Used) 缓存淘汰策略。

Downloads

9

Readme

CacheManager

CacheManager 是一个用于管理前端缓存的 JavaScript 类。它提供了一系列方法来存储、检索和清除缓存数据,支持使用 localStoragesessionStorage 作为存储介质,并实现了简单的 LRU (Least Recently Used) 缓存淘汰策略。

特性

  • 支持自定义缓存过期时间
  • 支持自定义序列化和反序列化方法
  • 可选使用 localStoragesessionStorage
  • 自动执行 LRU 缓存淘汰策略
  • 支持按 URL 匹配缓存
  • 提供缓存统计信息

使用方法

首先,你需要导入 CacheManager 类并创建一个实例:

import { cacheManager } from 'cache-manager';

const options = {
  matchText: 'example.com', // 自定义匹配文本
  serialize: customSerializeFunction, // 自定义序列化函数
  deserialize: customDeserializeFunction, // 自定义反序列化函数
  isUseLoaclStorage: true, // 是否使用 localStorage,默认为 false
};

const myCache = cacheManager(180, options); // 缓存过期时间(默认180分钟),配置项(option)

设置缓存

myCache.setCacheValue({
  value: 'some data',
  expiration: 300, // 可选,单位为分,默认为构造函数中设置的值
  flagKey: 'uniqueKey', // 可选,默认为 'default'
});

获取缓存

const cachedData = myCache.getCacheValue('uniqueKey');

清除缓存

myCache.clearCacheValue('uniqueKey'); // 清除特定键的缓存
myCache.clearCacheValue(); // 清除所有缓存

缓存统计

const stats = myCache.getCacheStats();
console.log(stats.text);

页面重载时清除缓存

myCache.clearOnPageReload();

注意事项

  • 缓存管理器默认匹配的文本是 'imdada.cn',你可以通过 options.matchText 来自定义。
  • 默认的序列化和反序列化方法是 JSON.stringifyJSON.parse,你可以通过 options.serializeoptions.deserialize 来自定义。
  • 缓存大小限制默认为 4MB,超过这个大小时将触发 LRU 淘汰策略。

贡献

如果你有任何建议或改进,请提交 Pull Request 或创建 Issue。

许可证

本项目采用 ISC 许可证。有关更多信息,请查看项目中的 LICENSE 文件。