npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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()