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

@lexriver/cache

v2.0.0

Published

This package provides

Downloads

4

Readme

Cache

This package provides

  • BoxedValue<T>
  • CachedVariable<T>
  • CachedMap<K,V>

for caching your data.

Install

npm install @lexriver/cache

CachedVariable<T>

    const myCachedVariable = new CachedVariable<string>(options)

where options is an object of type:

{
    maxAgeInMilliseconds:number, 
    updateFunction:(prevValue?:T)=>Promise<T>|T, 
    initialValue?:T
    useLazyUpdate?:boolean
}
  • maxAgeInMilliseconds:number is a timeout before updating the value
  • updateFunction:(prevValue?:T)=>Promise<T>|T is a function that will be executed to update value
  • initialValue?:T is a initial value, default is undefined
  • useLazyUpdate?:boolean if true then stale value will be updated only on getAsync() method

Example

    const myCachedVariable = new CachedVariable<string>({
        maxAgeInMilliseconds:5000, 
        updateFunction:(prevValue:string)=>{
            return prevValue+'+'
        } 
        initialValue:'default value'
    })

setUpdateFunction(updateFunctionAsync:(prevValue?:T)=>Promise<T>|T)

Change the update function

    myCachedVariable.setUpdateFunction((prevValue:string)=>prevValue+'!')

async getAsync():Promise<T>

Returns cached value if possible or executes updateFunctionAsync to get a new value

    await myCachedVariable.getAsync()

async updateWithValueAsync(value:T)

Set new value

    await myCachedVariable.updateWithValueAsync('my new value')

async clearCacheAsync()

Remove any existing value from cache

    await myCachedVariable.clearCacheAsync()

hasValue:boolean

Check if there is some value in cache

    if(myCachedVariable.hasValue) {...}

onDisposeEvent:TypeEvent<()=>void>

Event that triggers when clearCacheAsync() is executed

    myCachedVariable.onDisposeEvent.subscribe(() => {
        console.log('myCachedVariable is empty')
    })

CachedMap<K,V>

    const myCachedMap = new CachedMap<K, V>(options)

K is a type for key, and V is a value type.

K can be number, boolean, string, Object, or interface

options is an object of type:

{
    maxAgeInMilliseconds:number, 
    updateFunction:(key:K)=>Promise<V>|V, 
    useLazyUpdate?:boolean
}
  • maxAgeInMilliseconds:number is a timeout before updating the value
  • updateFunction:(key:K)=>Promise<V>|V is a function that will be executed to update value
  • useLazyUpdate?:boolean if true then stale value will be updated only on getAsync() method

Example

interface MyInterface{
    text:string
}
let myCachedMap2 = new CachedMap<MyInterface, number>({
    maxAgeInMilliseconds: 100*1000,
    updateFunction: async (key:MyInterface) => {
        let result = await makeSomeRequestAsync(key)
        // ..
        return result
    }
})

setUpdateFunction(updateFunctionAsync:(key:K)=>Promise<V>|V)

Change the update function.

    myCachedMap.setUpdateFunction((key:K) => key+'!')

async getAsync(key:K):Promise<V>

Get value by key.

    const result = await myCachedMap.getAsync()

clearCache()

Clear cache and force to update all values.

    myCachedMap.clearCache()

clearCacheForKey(key:K)

Force to update value only for specified key.

    myCahcedMap.clearCacheForKey('myKey')

BoxedValue<T>

BoxedValue is a wrapper for any value. Value can be undefined.

Example

    let x = new BoxedValue<number|undefined>()
    x.hasValue // false

    x.set(undefined)
    x.hasValue // true
    x.getOrThrow() // undefined

    x.set(100)
    x.getOrThrow() // 100

constructor

    let myBoxedValue = new BoxedValue<T>(initialValue?:T)

Example

    let myBoxedValue = new BoxedValue<number>(100)

get hasValue():boolean

Check if box has a value.

    myBoxedValue.hasValue // true or false

get isEmpty():boolean

Check if box is empty.

    myBoxedValue.isEmpty // true or false

set(value:T)

Set new value.

    myBoxedValue.set(100)

setByPrevious(action:(previousValue?:T)=>T)

Set new value by using previous value.

    myBoxedValue.setByPrevious((prev:number) => prev+1)

getOrThrow()

Return value if exists else throw an error.

    try {
        let currentValue = myBoxedValue.getOrThrow()
        console.log('value exists, currentValue=', currentValue)

    } catch(x){
        console.error('no value', x)

    }

getOrUndefined():T|undefined

Return value if exists else return undefined.

    const myBoxedValue = new BoxedValue<number>()
    myBoxedValue.getOrUndefined() // undefined
    myBoxedValue.set(100)
    myBoxedValue.getOrUndefined() // 100

deleteValue()

Delete value if exists.

    myBoxedValue.set(100)
    myBoxedValue.getOrUndefined() // 100
    myBoxedValue.deleteValue()
    myBoxedValue.hasValue //false
    myBoxedValue.getOrUndefined() // undefined

get lastModifactionDate()

Returns last modification date.

If no value has been set yet then retuns date when instance was created.

    console.log('last modification date = ', myBoxedValue.lastModificationDate)