@aadvdh/localstorage-cache 中文文档教程

发布于 3年前 浏览 19 更新于 3年前

localstorage-cache

一个小型库,用于向 localStorage 添加缓存功能。

Install

npm install @aadvdh/localstorage-cache

How to use

import the LocalStorageCache class

import LocalStorageCache from 'localstorage-cache.js'

Initialize a new instance

let ls = new LocalStorageCache();

参数: cacheExpiryTime - 将项目保留在缓存中的时间量(以秒为单位),标准为 3600 秒或 1 小时。 prefix - 添加到每个键的前缀。 标准值为“lsc”

Add an item to the cache:

ls.set(key,value,cacheExpiryTime);

如果超过最大存储大小,将自动清理缓存。 如果成功则返回 true,否则返回 false

Arguments: key - 检索值所依据的键。 去除任何非字母数字字符。 value - 要存储的值。 可以是任何值 cacheExpiryTime - 如果您想覆盖 cacheExpiryTime。

Retrieve an item from the cache:

ls.get(key);

如果该值存在且该项目尚未过期,则返回该值。 否则为空。

参数: key - 要检索的值的键。

setBucket:

ls.setBucket(bucket);

设置存储项目的 bucketname。所有非字母数字字符都将从字符串中删除。

参数: bucket - 要使用的桶的名称

Flush:

ls.flush(expired);

从存储中删除密钥而不影响所有 localStorage。 如果 expired 设置为 false 它将删除每个键,否则它只会删除过期的键。

参数 expired - 布尔值是只删除过期的项目还是删除所有项目。

Flush bucket

ls.flushBucket(expired, bucket)
Same as flush, but will only remove from a bucket.

参数: expired - 布尔值是只删除过期的项目还是删除所有项目。 bucket - 要从中删除的桶,否则它将使用类中定义的桶。

localstorage-cache

A small library to add caching capabilities to localStorage.

Install

npm install @aadvdh/localstorage-cache

How to use

import the LocalStorageCache class

import LocalStorageCache from 'localstorage-cache.js'

Initialize a new instance

let ls = new LocalStorageCache();

Arguments: cacheExpiryTime - The amount of time to keep an item in the cache in seconds, standard at 3600 seconds, or 1 hour. prefix - a prefix added to every key. Standard value is "lsc"

Add an item to the cache:

ls.set(key,value,cacheExpiryTime);

Will automatically clean up the cache incase the maximum storage size is exceeded. returns true if succesful, false otherwise

Arguments: key - a key to retrieve the value by. Any non alphanumeric characters are stripped. value - the value to store. Can be any value cacheExpiryTime - in case you want to override the cacheExpiryTime.

Retrieve an item from the cache:

ls.get(key);

returns the value if it exists and the item hasn't expired yet. Null otherwise.

Arguments: key - the key of the value to be retrieved.

setBucket:

ls.setBucket(bucket);

Set the bucketname to store the items in. All non alphanumeric characters are removed from the string.

Arguments: bucket - name of the bucket to use

Flush:

ls.flush(expired);

Removes keys from storage without affecting all of the localStorage. If expired is set to false it will remove every key, otherwise it will only remove expired keys.

Arguments expired - Boolean value whether only expired items should be removed or all items.

Flush bucket

ls.flushBucket(expired, bucket)
Same as flush, but will only remove from a bucket.

Arguments: expired - Boolean value whether only expired items should be removed or all items. bucket - The bucket to remove from, otherwise it will use the bucket defined within the class.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文