feathers-cache
v0.2.7
Published
Client-side feathers services to cache data from remote services in memory or on disk.
Downloads
6
Readme
feathers-cache
Client-side feathers services to cache data from remote services in memory or on disk. Memory cache is simply an array. Disk cache is driven by localForage and localForage-cordovaSQLiteDriver, using cordova SQLite, IndexedDB, WebSQL or localStorage in turn when available.
Install
npm i -S feathers-cache
Usage
First, set up feathers
environment both on your server and in your client codes. See the docs for detail.
Then on client-side: (in coffeescript syntax. You know how to write in JS. :) )
cache = require 'feathers-cache'
cachedService = app.use 'yourCachedServiceName', cache
service: 'xxx' # the real feathers service name it caches from
id: 'id' # id field name, '_id' by default
cache:
memory: # get from memory first. no expiration verification since we have socke.io push.
length: 100 # max memory cache length. 200 by default.
find: true # if find **only** from memory cache, ignoring real feathers. false by default.
disk: # if memory failed, try get from disk. fetch a timestamp to verify expiration.
length: 500 # max disk cache length. 1000 by default.
timestamp: xx # timestamp field name for expiration verification. 'updatedAt' by default.
name: 'xx' # disk cache storage name. 'feathers-cache' by default.
driver: [...] # disk cache drivers. see <localforge.setDriver>. default value works ok.
init: # initialize this cache by find with this init query
field: value
$limit: xx
$skip: xx
$sort: ...
$select: ...
debug: true # print debug info to cosole. false by default.
methods: # define new methods on this service besides get/find/create/...
method1: ...
method2: ...
onsetup: ... # run this when the wervice is setup (before init query is executed).
onready: ... # run this when the service is ready (after init query is completed).
app.setup() # initializate the services.
Now cachedService
is a new client-side service to cache and proxy the real remote service. All caching behaviors are transparent to user, so it can be treated as the real remote service itself. In fact it can also be used as your front-end data model layer, and you can write your data processing logics into the methods
part.
Available APIs:
# standard feathers service methods, proxying the real remote service's methods, with cache enabled.
cachedService.get/find/create/update/patch/remove ...
# your own methods defined in the 'methods' part
cachedService.method1/method2 ...
# a promise indicating if the service is initialized.
cachedService.ready
.then (data) => ... # get initial data here
# standard feathers service events
cachedService.on 'created'/'updated'/'patched'/'removed', (data) => ...
# reading/writing events: emit if there are any on-going reading/writing tasks
cachedService.on 'reading'/'writing', (boolean) => ...