Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <h1 align="center">
  2. <img width="250" src="https://rawgit.com/lukechilds/keyv/master/media/logo.svg" alt="keyv">
  3. <br>
  4. <br>
  5. </h1>
  6. > Simple key-value storage with support for multiple backends
  7. [![Build Status](https://travis-ci.org/lukechilds/keyv.svg?branch=master)](https://travis-ci.org/lukechilds/keyv)
  8. [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv?branch=master)
  9. [![npm](https://img.shields.io/npm/dm/keyv.svg)](https://www.npmjs.com/package/keyv)
  10. [![npm](https://img.shields.io/npm/v/keyv.svg)](https://www.npmjs.com/package/keyv)
  11. Keyv provides a consistent interface for key-value storage across multiple backends via storage adapters. It supports TTL based expiry, making it suitable as a cache or a persistent key-value store.
  12. ## Features
  13. There are a few existing modules similar to Keyv, however Keyv is different because it:
  14. - Isn't bloated
  15. - Has a simple Promise based API
  16. - Suitable as a TTL based cache or persistent key-value store
  17. - [Easily embeddable](#add-cache-support-to-your-module) inside another module
  18. - Works with any storage that implements the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) API
  19. - Handles all JSON types plus `Buffer`
  20. - Supports namespaces
  21. - Wide range of [**efficient, well tested**](#official-storage-adapters) storage adapters
  22. - Connection errors are passed through (db failures won't kill your app)
  23. - Supports the current active LTS version of Node.js or higher
  24. ## Usage
  25. Install Keyv.
  26. ```
  27. npm install --save keyv
  28. ```
  29. By default everything is stored in memory, you can optionally also install a storage adapter.
  30. ```
  31. npm install --save @keyv/redis
  32. npm install --save @keyv/mongo
  33. npm install --save @keyv/sqlite
  34. npm install --save @keyv/postgres
  35. npm install --save @keyv/mysql
  36. ```
  37. Create a new Keyv instance, passing your connection string if applicable. Keyv will automatically load the correct storage adapter.
  38. ```js
  39. const Keyv = require('keyv');
  40. // One of the following
  41. const keyv = new Keyv();
  42. const keyv = new Keyv('redis://user:pass@localhost:6379');
  43. const keyv = new Keyv('mongodb://user:pass@localhost:27017/dbname');
  44. const keyv = new Keyv('sqlite://path/to/database.sqlite');
  45. const keyv = new Keyv('postgresql://user:pass@localhost:5432/dbname');
  46. const keyv = new Keyv('mysql://user:pass@localhost:3306/dbname');
  47. // Handle DB connection errors
  48. keyv.on('error', err => console.log('Connection Error', err));
  49. await keyv.set('foo', 'expires in 1 second', 1000); // true
  50. await keyv.set('foo', 'never expires'); // true
  51. await keyv.get('foo'); // 'never expires'
  52. await keyv.delete('foo'); // true
  53. await keyv.clear(); // undefined
  54. ```
  55. ### Namespaces
  56. You can namespace your Keyv instance to avoid key collisions and allow you to clear only a certain namespace while using the same database.
  57. ```js
  58. const users = new Keyv('redis://user:pass@localhost:6379', { namespace: 'users' });
  59. const cache = new Keyv('redis://user:pass@localhost:6379', { namespace: 'cache' });
  60. await users.set('foo', 'users'); // true
  61. await cache.set('foo', 'cache'); // true
  62. await users.get('foo'); // 'users'
  63. await cache.get('foo'); // 'cache'
  64. await users.clear(); // undefined
  65. await users.get('foo'); // undefined
  66. await cache.get('foo'); // 'cache'
  67. ```
  68. ### Custom Serializers
  69. Keyv uses [`json-buffer`](https://github.com/dominictarr/json-buffer) for data serialization to ensure consistency across different backends.
  70. You can optionally provide your own serialization functions to support extra data types or to serialize to something other than JSON.
  71. ```js
  72. const keyv = new Keyv({ serialize: JSON.stringify, deserialize: JSON.parse });
  73. ```
  74. **Warning:** Using custom serializers means you lose any guarantee of data consistency. You should do extensive testing with your serialisation functions and chosen storage engine.
  75. ## Official Storage Adapters
  76. The official storage adapters are covered by [over 150 integration tests](https://travis-ci.org/lukechilds/keyv/jobs/260418145) to guarantee consistent behaviour. They are lightweight, efficient wrappers over the DB clients making use of indexes and native TTLs where available.
  77. Database | Adapter | Native TTL | Status
  78. ---|---|---|---
  79. Redis | [@keyv/redis](https://github.com/lukechilds/keyv-redis) | Yes | [![Build Status](https://travis-ci.org/lukechilds/keyv-redis.svg?branch=master)](https://travis-ci.org/lukechilds/keyv-redis) [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv-redis/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv-redis?branch=master)
  80. MongoDB | [@keyv/mongo](https://github.com/lukechilds/keyv-mongo) | Yes | [![Build Status](https://travis-ci.org/lukechilds/keyv-mongo.svg?branch=master)](https://travis-ci.org/lukechilds/keyv-mongo) [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv-mongo/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv-mongo?branch=master)
  81. SQLite | [@keyv/sqlite](https://github.com/lukechilds/keyv-sqlite) | No | [![Build Status](https://travis-ci.org/lukechilds/keyv-sqlite.svg?branch=master)](https://travis-ci.org/lukechilds/keyv-sqlite) [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv-sqlite/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv-sqlite?branch=master)
  82. PostgreSQL | [@keyv/postgres](https://github.com/lukechilds/keyv-postgres) | No | [![Build Status](https://travis-ci.org/lukechilds/keyv-postgres.svg?branch=master)](https://travis-ci.org/lukechildskeyv-postgreskeyv) [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv-postgres/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv-postgres?branch=master)
  83. MySQL | [@keyv/mysql](https://github.com/lukechilds/keyv-mysql) | No | [![Build Status](https://travis-ci.org/lukechilds/keyv-mysql.svg?branch=master)](https://travis-ci.org/lukechilds/keyv-mysql) [![Coverage Status](https://coveralls.io/repos/github/lukechilds/keyv-mysql/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/keyv-mysql?branch=master)
  84. ## Third-party Storage Adapters
  85. 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.
  86. ```js
  87. const Keyv = require('keyv');
  88. const myAdapter = require('./my-storage-adapter');
  89. const keyv = new Keyv({ store: myAdapter });
  90. ```
  91. Any store that follows the [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) api will work.
  92. ```js
  93. new Keyv({ store: new Map() });
  94. ```
  95. For example, [`quick-lru`](https://github.com/sindresorhus/quick-lru) is a completely unrelated module that implements the Map API.
  96. ```js
  97. const Keyv = require('keyv');
  98. const QuickLRU = require('quick-lru');
  99. const lru = new QuickLRU({ maxSize: 1000 });
  100. const keyv = new Keyv({ store: lru });
  101. ```
  102. The following are third-party storage adapters compatible with Keyv:
  103. - [quick-lru](https://github.com/sindresorhus/quick-lru) - Simple "Least Recently Used" (LRU) cache
  104. - [keyv-file](https://github.com/zaaack/keyv-file) - File system storage adapter for Keyv
  105. - [keyv-dynamodb](https://www.npmjs.com/package/keyv-dynamodb) - DynamoDB storage adapter for Keyv
  106. ## Add Cache Support to your Module
  107. Keyv is designed to be easily embedded into other modules to add cache support. The recommended pattern is to expose a `cache` option in your modules options which is passed through to Keyv. 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.
  108. You should also set a namespace for your module so you can safely call `.clear()` without clearing unrelated app data.
  109. Inside your module:
  110. ```js
  111. class AwesomeModule {
  112. constructor(opts) {
  113. this.cache = new Keyv({
  114. uri: typeof opts.cache === 'string' && opts.cache,
  115. store: typeof opts.cache !== 'string' && opts.cache,
  116. namespace: 'awesome-module'
  117. });
  118. }
  119. }
  120. ```
  121. Now it can be consumed like this:
  122. ```js
  123. const AwesomeModule = require('awesome-module');
  124. // Caches stuff in memory by default
  125. const awesomeModule = new AwesomeModule();
  126. // After npm install --save keyv-redis
  127. const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
  128. // Some third-party module that implements the Map API
  129. const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });
  130. ```
  131. ## API
  132. ### new Keyv([uri], [options])
  133. Returns a new Keyv instance.
  134. The Keyv instance is also an `EventEmitter` that will emit an `'error'` event if the storage adapter connection fails.
  135. ### uri
  136. Type: `String`<br>
  137. Default: `undefined`
  138. The connection string URI.
  139. Merged into the options object as options.uri.
  140. ### options
  141. Type: `Object`
  142. The options object is also passed through to the storage adapter. Check your storage adapter docs for any extra options.
  143. #### options.namespace
  144. Type: `String`<br>
  145. Default: `'keyv'`
  146. Namespace for the current instance.
  147. #### options.ttl
  148. Type: `Number`<br>
  149. Default: `undefined`
  150. Default TTL. Can be overridden by specififying a TTL on `.set()`.
  151. #### options.serialize
  152. Type: `Function`<br>
  153. Default: `JSONB.stringify`
  154. A custom serialization function.
  155. #### options.deserialize
  156. Type: `Function`<br>
  157. Default: `JSONB.parse`
  158. A custom deserialization function.
  159. #### options.store
  160. Type: `Storage adapter instance`<br>
  161. Default: `new Map()`
  162. The storage adapter instance to be used by Keyv.
  163. #### options.adapter
  164. Type: `String`<br>
  165. Default: `undefined`
  166. Specify an adapter to use. e.g `'redis'` or `'mongodb'`.
  167. ### Instance
  168. Keys must always be strings. Values can be of any type.
  169. #### .set(key, value, [ttl])
  170. Set a value.
  171. By default keys are persistent. You can set an expiry TTL in milliseconds.
  172. Returns `true`.
  173. #### .get(key)
  174. Returns the value.
  175. #### .delete(key)
  176. Deletes an entry.
  177. Returns `true` if the key existed, `false` if not.
  178. #### .clear()
  179. Delete all entries in the current namespace.
  180. Returns `undefined`.
  181. ## License
  182. MIT © Luke Childs