stock-cache
v1.0.0
Published
High cache structuring
Downloads
2
Readme
Stock Objects
Store objects in a structured way with membership and groupings in memory cache
Get started
const Stock = require('stock-cache')
const stock = new Stock()
// Use the same instance in every application
Understanding entities and attributes
| Entity | Description | | ------ | ----------- | | stock | General and single instance control and creation of objects | | cache | Instance for controlling access to objects | | class | String classifier of objects | | key | String identifier of objects | | child | An object can be a child of another | | array | An object can have hashed arrays of itens | | group | Objects can belong to groups for easier searching |
Stocking up objects
Set object
const myObject = { brand: 'ferrari', model: '250 GTO' }
const myObjectId = '250gto'
const cache = stock.cache()
cache.class('car').key(myObjectId)
await cache.set(myObject)
Get object
const myObjectId = '250gto'
const cache = stock.cache()
cache.class('car').key(myObjectId)
const myObject = await cache.get()
console.log(myObject)
// { brand: 'ferrari', model: '250 GTO' }
Update object
const myObjectId = '250gto'
const cache = stock.cache()
cache.class('car').key(myObjectId)
cache.update({ model: '250 GT', power: '296 hp; 221 kW' })
console.log(await cache.get())
// { brand: 'ferrari', model: '250 GT', power: '296 hp; 221 kW' }
Delete object
const myObjectId = '250gto'
const cache = stock.cache()
cache.class('car').key(myObjectId)
await cache.del()
console.log(await cache.get())
// undefined
Array in object
const myObjectId = '250gto'
const cache = stock.cache()
cache.class('car').key(myObjectId).array('championships')
/* append new item */
await cache.append('year1963', { place: '...' })
await cache.append('year1964', { place: '...' })
/* pre append new item */
await cache.prepend('year1962', { place: '...' })
/* append many */
await cache.appendMany([
[ 'year1965', { place: '...' } ],
[ 'year1966', { place: '...' } ]
])
/* pre append many */
await cache.prependMany([
[ 'year1960', { place: '...' } ],
[ 'year1961', { place: '...' } ]
])
/* get item */
const item = await cache.getItem('year1962')
/* set item */
await cache.setItem('year1962', { news: 'item' })
/* get first item */
const item = await cache.firstItem()
const itemId = await cache.firstItemId()
/* get last item */
const item = await cache.lastItem()
const itemId = await cache.lastItemId()
/* delete item */
await cache.delItem('year1962')
/* list items */
const championships = await cache.list()
/* list items IDs */
const championshipsIds = await cache.listIds()
/* size of list */
const size = await cache.size()
Childs of object
const brand = { name: 'ferrari', country: 'italy' }
const brandId = 'ferrari'
const cacheBrand = stock.cache()
cacheBrand.class('brand').key(brandId)
await cacheBrand.set(brand)
const car = { brand: 'ferrari', model: '250 GTO' }
const carId = '250gto'
const cacheCar = stock.cache()
cacheCar.class('car').key(carId)
await cacheCar.set(car)
/* Setting child class name to start */
cacheBrand.child('car')
/* Add child */
cacheBrand.addChild(cacheCar)
await cacheBrand.save()
/* Remove child of */
cacheBrand.remChild(cacheCar)
await cacheBrand.save()
/* Is child of */
const isChild = await cacheCar.isChild(cacheBrand)
console.log(isChild) // true
/* Clean all childs of */
await cacheBrand.cleanChilds()
/* List caches of childs */
const carsCaches = await cacheBrand.childs()
/* List values of childs */
const cars = await cacheBrand.childValues()
/* List keys of childs */
const carsIds = await cacheBrand.childKeys()
/* List all childs from object */
/* List all childs caches of object, regardless of the defined class */
const allChilds = await cacheBrand.allChilds()
/* List all childs values of object, regardless of the defined class */
const allChildValues = await cacheBrand.allChildValues()
/* List all childs keys of object, regardless of the defined class */
const allChildKeys = await cacheBrand.allChildKeys()
/* List all childs from class */
/* List all childs caches of class, regardless of the defined class */
const allClassChilds = await cacheBrand.allClassChilds()
/* List all childs values of class, regardless of the defined class */
const allClassChildsValues = await cacheBrand.allClassChildsValues()
/* List all childs keys of class, regardless of the defined class */
const allClassChildsKeys = await cacheBrand.allClassChildsKeys()
List objects from class
const cache = stock.cache()
cache.class('car')
/* List all caches in class */
const caches = await cache.allClass()
/* List all values in class */
const values = await cache.allClassValues()
/* List all keys in class */
const keys = await cache.allClassKeys()
Groups of objects
const car1 = { brand: 'ferrari', model: '250 GTO' }
const car1Id = '250gto'
const cacheCar1 = stock.cache()
cacheCar1.class('car').key(car1Id)
await cacheCar1.set(car1)
const car2 = { brand: 'ferrari', model: 'Sf90' }
const car2Id = 'sf90'
const cacheCar2 = stock.cache()
cacheCar2.class('car').key(car2Id)
await cacheCar2.set(car2)
/* Add in group */
await cacheCar1.addGroup('sporting').save()
await cacheCar2.addGroup('sporting').save()
/* Remove from group */
await cacheCar2.remGroup('sporting').save()
/* List group names from object */
const groups = await cacheCar1.groups()
console.log(groups) // [ 'sporting' ]
/* Cache to list groups */
const cache = stock.cache()
cache.class('car').group('sporting')
/* List call caches from group */
const caches = await cache.allGroup()
/* List call values from group */
const values = await cache.allGroupValues()
/* List call keys from group */
const keys = await cache.allGroupKeys()