Codync
무료 오픈소스 1:1 대안, Grok Bot과 Muse 대신
Simple key-value storage for Node.js with TTL and pluggable backends via Keyv adapters.
저장소 README에서 가져옴
README 전체를 불러오는 중입니다.
README 전문 보기 ↗Keyv is a simple key-value storage with support for multiple backend adapters (MySQL, PostgreSQL, SQLite, Redis, Mongo, DynamoDB, Firestore, Memcached, and more).
Forked from keyv, plus:
Buffer.npm install @keyvhq/core --save
You can optionally install the storage adapter you want to use:
npm install @keyvhq/redis --save
npm install @keyvhq/mongo --save
npm install @keyvhq/sqlite --save
npm install @keyvhq/postgres --save
npm install @keyvhq/mysql --save
If you don't provide a specific storage adapter, a in-memory storage adapter is used by default.
Just create a new Keyv instance, using an specific storage adapter:
const keyv = new Keyv() // in-memory, by default
const keyvRedis = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379')})
const keyvMongo = new Keyv({ store: new KeyvMongo('mongodb://user:pass@localhost:27017/dbname')})
const keyvSQLite = new Keyv({ store: new KeyvSQLite('sqlite://path/to/database.sqlite')})
const keyvPostgreSQL = new Keyv({ store: new KeyvPostgreSQL('postgresql://user:pass@localhost:5432/dbname')})
const keyvMySQL = new Keyv({ store: new KeyvMySQL('mysql://user:pass@localhost:3306/dbname')})
await keyv.set('foo', 'expires in 1 second', 1000) // true
await keyv.set('foo', 'never expires') // true
await keyv.get('foo') // 'never expires'
await keyv.has('foo') // true
await keyv.delete('foo') // true
await keyv.has('foo') // false
await keyv.clear() // undefined
You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.
const users = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'users' })
const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379'), namespace: 'cache' })
await users.set('foo', 'users') // true
await cache.set('foo', 'cache') // true
await users.get('foo') // 'users'
await cache.get('foo') // 'cache'
await users.clear() // undefined
await users.get('foo') // undefined
await cache.get('foo') // 'cache'
Keyv serializes data as JSON, encoding Buffers the same way json-buffer does, to ensure consistency across different backends.
You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.
The following example is using @keyvhq/compress as serializer:
const KeyvCompress = require('@keyvhq/compress')
const Keyv = require('@keyvhq/core')
const keyv = KeyvCompress(
new Keyv({
serialize: v8.serialize,
deserialize: v8.deserialize
})
)
Keyv is designed to be easily embedded into other modules to add cache support.
Caching will work in memory by default and users have the option to also install a Keyv storage adapter and pass in a connection string, or any other storage that implements the Map API.
const KeyvRedis = require('@keyvhq/redis')
const Keyv = require('@keyvhq/core')
const got = require('got')
const cache = new Keyv({ store: new KeyvRedis('redis://user:pass@localhost:6379') })
await got('https://keyvhq.js.org', { cache })
The recommended pattern is to expose a cache option in your modules options which is passed through to Keyv.
For example, quick-lru is a completely unrelated module that implements the Map API.
const Keyv = require('@keyvhq/core')
const QuickLRU = require('quick-lru')
const lru = new QuickLRU({ maxSize: 1000 })
const keyv = new Keyv({ store: lru })
You should also set a namespace for your module so you can safely call .clear() without clearing unrelated app data.
The official storage adapters are covered by over 150 integration tests to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
You can also use third-party storage adapters or build your own. Keyv will wrap these storage adapters in TTL functionality and handle complex types internally.
Returns a new Keyv instance.
Type: Object
The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
Type: String
Default: undefined
Namespace for the current instance.
Type: Number
Default: undefined
Default TTL in milliseconds. Can be overridden by specifying a TTL on .set().
Type: Function
Default: JSON.stringify with Buffers stored as :base64: strings
A custom serialization function.
Type: Function
Default: JSON.parse, restoring :base64: strings as Buffers
A custom deserialization function.
The default pair escapes strings that start with : by adding one more :, and the default deserializer removes it again. Set serialize and deserialize together, so stored values are read with the format they were written in.
Type: Storage adapter instance
Default: new Map()
The storage adapter instance to be used by Keyv.
Type: Boolean
Default: false
If set to true the raw DB object Keyv stores internally will be returned instead of just the value.
This contains the TTL timestamp.
Set a value.
By default keys are persistent. You can set an expiry TTL in milliseconds.
Returns a promise which resolves to true.
Returns a promise which resolves to the retrieved value.
Returns a promise which resolves to a boolean, indicating existence of a key.
Deletes an entry.
Returns a promise which resolves to true if the key existed, false if not.
Delete all entries in the current namespace.
Returns a promise which is resolved when the entries have been cleared.
When calling clear(), on a keyv instance with no namespace, all keys are cleared.
Returns an async iterator, which iterates over all the key-value pairs in the namespace. When called without a namespace, it iterates over all entries in the database.
The iterator shouldn't be used in environments where performance is key, or there are more than 1000 entries in the database, use an ORM or a native driver if you need to iterate over all entries.
keyv © Luke Childs, released under the MIT License. Maintained by Microlink with help from contributors.
microlink.io · GitHub microlinkhq · X @microlinkhq