在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mkozjak/koa-cache-lite开源软件地址:https://github.com/mkozjak/koa-cache-lite开源编程语言:JavaScript 100.0%开源软件介绍:koa-cache-liteZero-dependency koa router cache Newsimportant Breaking API changes were made since version 3.x. Once included, the library returns the 'configure', 'init', 'middleware', 'exists', 'clear' and 'currentCacheType' methods. 'configure' is used to configure caching, and 'middleware' to get the generator which can be used with koa.use(). 'init' is an optional method for the initialization of the caching process. This is a convenience method used, for example, in a 'cluster' environment, where you would send a wrapped 'middleware' function to be able to, in example, clear cache from the worker, even though the cache was initialized in the master process. 'clear' can be used to clear the cache and 'currentCacheType' to get the currently used caching system. See below for API details and examples. InstallationLocally in your project or globally:
Quick start'use strict'
var cache = require('koa-cache-lite')
var koa = require('koa')()
// use in-memory cache
cache.configure({
'/api/v1/test': 3000
}, {
debug: true
})
// or redis (install ioredis manually with npm install ioredis)
cache.configure({
'/api/v1/test': 3000
}, {
external: {
type: 'redis',
host: '127.0.0.1',
port: 6379
},
debug: true
})
// use increasing cache timeout and customize cache key
cache.configure({
'/api/v1/test': 'increasing'
}, {
increasing: {
1: 1000, // key = route calls per minute, value = cache timeout
5: 2000,
25: 5000,
100: 10000
},
keyFragment: 'cache', // header field containing cache key appendix
debug: true
})
// start caching
koa.use(cache.middleware())
koa.listen(1337) APIkoa-cache-lite accepts two arguments. First one is an object consisting of routes and the second one is an options object, having an 'external' key where a type of cache and its location are defined, cache.configure(routes[, options]) -> ClassSets various caching options regarding urls, internal cache keys, debug mode and more. routes {Object}:
cache.configure({
...
routes: {
{String}: {String|Number|Boolean}
}
})
// or
cache.configure({
...
routes: {
{String}: {
timeout: {String|Number}
cacheKeyArgs: {
headers: {String|Array},
query: {String|Array}
}
}
}
})
// in example
cache.configure({
...
routes: {
// will cache based on parameter 'id'
'/api/client/:id/tickets': 30000,
// will cache based on parameter 'uid' and an http header field 'x-auth-custom'
'/api/admin/register/:uid': {
timeout: 10000,
cacheKeyArgs: {
headers: 'x-auth-custom'
}
}
}
}) options {Object}:
cache.init() -> ClassStart the caching process. This method should only be used when different contexts throughout your application are used.
In example, when using the 'cluster' module, you should use cache.init() cache.middleware() -> GeneratorStarts the caching process and returns a generator which can be used in conjunction with koa.use(). koa.use(cache.middleware()) cache.exists(key) -> PromiseCheck if cache for the given key exists. cache.clear([keys])Clear cache entries individually or all of them. If 'keys' is not provided it clears all the keys based on cacheKeyPrefix if defined. It also supports glob-style patterns:
// clear all keys (based on cacheKeyPrefix)
cache.clear()
// or only /api/v1/users
cache.clear('/api/v1/users')
// or anything under '/api/v2'
cache.clear('/api/v2/*') cache.currentCacheType() -> StringReturns a type of caching system currently used. Professional supportI offer professional support for koa-cache-lite and beyond. I have many years of expertise on building robust, scalable Node.js applications and can help you overcome issues and challenges preventing you to ship your great products. I also excel in software architecture and implementation, being able to provide you with development, planning, consulting, training and customization services. Feel free to contact me so we can discuss how to help you finish your products! SponsorsBecome a sponsor and get your logo on project's README on GitHub with a link to your site. Feel free to contact me for the arrangement! DonateIf you love this project, consider donating! License |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论