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

@floatsheep/cachemanager

v1.0.13

Published

[中文(简体)](#中文简体) | English

Downloads

5

Readme

中文(简体) | English

cacheManager

A simple persistent store based on the browser Cache API.

The item ~~copied~~ referenced CrazyCreativeDream/CacheDB and rewrite it with typescript.

Package size

  • cachemanager.js: ~4kB & gzip: ~1kB

  • cachemanager.umd.cjs: ~3kB & gzip: ~1kB

Usage

Example

const cacheManager = new CacheManager({
  cachePrefix: "myApp",
  cacheNamespace: "userData",
  broadId: "project1",
}); // Configuration(optional)

cacheManager.init(); // Initialize(required)

await cacheManager.setItem("foo", "bar", "text/plain"); // return true;

await cacheManager.getItem("foo"); // return "bar";

await cacheManager.removeItem("foo"); // return true;

If you change mime-types

await cacheManager.setItem("foo", 114514, "text/plain"); // return true;

await cacheManager.getItem("foo"); // return "114514";

await cacheManager.setItem("foo", 114514, "text/number"); // return true;

await cacheManager.getItem("foo"); // return 114514;

[!TIP] Note that mime-types cannot be converted from one type to another.

For example,

await cacheManager.setItem("foo", "{version: 1}", "application/json"); // return true;

await cacheManager.getItem("foo"); // return '{version: 1}';

await cacheManager.setItem("foo", "1", "text/number"); // return true;

await cacheManager.getItem("foo"); // return NaN;

Instead,

await cacheManager.setItem("foo", { version: 1 }, "text/plain"); // return true;

await cacheManager.getItem("foo"); // return '{version: 1}';

Why do these problems arise?

The mime-types item in the parameter will be stored in headers, and the data will be processed according to this parameter when reading (i.e., call response.json(), response.text(), etc.) instead of forcing the type conversion.

cacheManager supports type parameters, but note that type assertions or judgments are required to handle null cases

await cacheManager.setItem("foo", "<p>HTML Element</p>", "text/html"); // return true;

const bar = await cacheManager.getItem<string>("foo");
const element = document.getElementById("app");
if (element) {
  element.innerHTML = bar!.split('"').filter(e => e !== "")[0];
}

When cacheManager does not fetch data, null is returned.

await cacheManager.getItem("foo"); // return null

WebWorker

Web Worker can be used in two ways.

Using in WebWorker with importScripts

importScripts("https://unpkg.com/@floatsheep/cachemanager"); // Using default

or

importScripts(
  "https://unpkg.com/@floatsheep/cachemanager@latest/dist/cachemanager.umd.cjs"
); // Using umd

Using in WebWorker with import

[!WARNING] This method relies on vite(rollover), webpack, or other build tools and it is not part of the WebWorker standard.

First, install the package.

npm install @floatsheep/cachemanager

Then, import it.

import { CacheManager } from "@floatsheep/cachemanager";

Using in WebScript(inline script and external script)

<script module>
  import { CacheManager } from "https://unpkg.com/@floatsheep/cachemanager"; // Using default
</script>

or

<script module>
  import { CacheManager } from "https://unpkg.com/@floatsheep/cachemanager@latest/dist/cachemanager.umd.cjs"; // Using umd
</script>

中文(简体)

一个基于浏览器 Cache API 的简单存储

这个项目~~抄袭~~参考了 CrazyCreativeDream/CacheDB,并使用 TypeScript 重写。

包体积

  • cachemanager.js: ~4kB & gzip: ~1kB

  • cachemanager.umd.cjs: ~3kB & gzip: ~1kB

使用方法

例子

const cacheManager = new CacheManager({
  cachePrefix: "myApp",
  cacheNamespace: "userData",
  broadId: "project1",
}); // 配置(可选)

cacheManager.init(); // 初始化(必须,否则报错)

await cacheManager.setItem("foo", "bar", "text/plain"); // 返回 true

await cacheManager.getItem("foo"); // 返回 "bar"

await cacheManager.removeItem("foo"); // 返回 true

如果更改 mime-types 参数

await cacheManager.setItem("foo", 114514, "text/plain"); // 返回 true

await cacheManager.getItem("foo"); // 返回 "114514"

await cacheManager.setItem("foo", 114514, "text/number"); // 返回 true

await cacheManager.getItem("foo"); // 返回 114514

[!TIP] 需要注意的是,mime-types 参数不能将一个类型强制转换为另一个类型

例如,

await cacheManager.setItem("foo", "{version: 1}", "application/json"); // return true;

await cacheManager.getItem("foo"); // 返回 '{version: 1}';

await cacheManager.setItem("foo", "1", "text/number"); // 返回 true;

await cacheManager.getItem("foo"); // 返回 NaN;

相反,

await cacheManager.setItem("foo", { version: 1 }, "text/plain"); // 返回 true;

await cacheManager.getItem("foo"); // 返回 '{version: 1}';

为什么会出现这些问题?

mime-types 参数将储存在 header 中,并且根据此参数来处理数据(即调用 response.json()、response.text()等),而不是强制类型转换。

cacheManager 支持类型参数,但要注意的是,需要使用类型断言或判断来处理 null 的情况

await cacheManager.setItem("foo", "<p>HTML Element</p>", "text/html"); // 返回 true;

const bar = await cacheManager.getItem<string>("foo");
const element = document.getElementById("app");
if (element) {
  element.innerHTML = bar!.split('"').filter(e => e !== "")[0];
}

当 cacheManager 没有获取到数据时,会返回 null

await cacheManager.getItem("foo"); // 返回 null

WebWorker

Web Worker 有两种方法使用

在 WebWorker 中以 importScripts 使用

importScripts("https://unpkg.com/@floatsheep/cachemanager"); // 使用默认版

or

importScripts(
  "https://unpkg.com/@floatsheep/cachemanager@latest/dist/cachemanager.umd.cjs"
); // 使用 umd(即通用模块规范)

如果你的项目没有特殊兼容需求,可以使用默认版本,否则请使用 umd 以获取更好的兼容性

在 WebWorker 中以 import 使用

[!WARNING] 这种方法需要依赖 vite(rollup)、webpack 等构建工具,并且它不是 WebWorker 规范的一部分

首先安装依赖

npm install @floatsheep/cachemanager

然后导入

import { CacheManager } from "@floatsheep/cachemanager";

在 WebScript 中使用

<script module>
  import { CacheManager } from "https://unpkg.com/@floatsheep/cachemanager"; // 使用默认版
</script>

or

<script module>
  import { CacheManager } from "https://unpkg.com/@floatsheep/cachemanager@latest/dist/cachemanager.umd.cjs"; // 使用 umd(即通用模块规范)
</script>