1.4.1 • Published 4 months ago

@coool/cachestate v1.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@coool/cachestate

A simple-to-use, minimal boilerplate flexible cached state.

Features

✅ Caching ✅ State Management ✅ Updates for cache consumers ✅ Works with any framework ✅ Local Storage Support ✅ Custom Storage Asynchronous Support ✅ Handles Simultaneous Requests ✅ Automatic & Manual Cache Busting ✅ Aspect-Oriented (Decorators) ✅ Out-of-the-box LocalStorage and SessionStorage cache data storage

Install

$ npm i --save @coool/cachestate

Basic Usage

Add CacheState to a function

import { CacheState } from '@coool/cachestate';

@CacheState()
function getItem$(itemId: ItemId): Observable<Item> {
  // Get latest version of item from the server
}

Consume CacheState

getItem$('1')
  .subscribe(item => {
    // You'll receive initial cached value and updates here
  });

Notify the CacheState that the value needs updating

import { CacheState, CacheStateUpdater } from '@coool/cachestate';

@CacheState({
  updatedNotifierKey: 'items-updated',
})
function getItem$(itemId: ItemId): Observable<Item> {
  // Get latest version of item from the server
}

@CacheStateUpdater({
  updatedNotifierKey: 'items-updated',
})
function updateItem() {
  // This will invalidate the cache and call the getItem$ function again then update cache consumers with the latest value 
}

Use-cases

Global Configuration

You can set configurations globally across all CacheStates. Local configuration will take precedence over global configuration.

import { GlobalCacheStateConfig } from '@coool/cachestate';

GlobalCacheStateConfig.maxAgeMS = 5 * 60 * 1000;

Invalidate cache without requesting update

import { CacheState, CacheStateInvalidator } from '@coool/cachestate';
import { Subject } from 'rxjs';

const cacheInvalidatedNotifier = new Subject<void>();

@CacheState({
  invalidatedNotifier: cacheInvalidatedNotifier,
})
function getItem$(itemId: ItemId): Observable<Item> {
}

@CacheStateInvalidator({
  invalidatedNotifier: cacheInvalidatedNotifier,
})
function updateItem() {
  // This will invalidate the cache and call the getItem$ function again then update cache consumers with the latest value 
}

Invalidate all cache globally

import { invalidateAllCache } from '@coool/cachestate';

invalidateAllCache();

Invalidate and update all cache globally

import { invalidateAndUpdateAllCache } from '@coool/cachestate';

invalidateAndUpdateAllCache();

LocalStorage data storage

import { GlobalCacheStateConfig, BrowserStorageCacheDataStorage } from '@coool/cachestate';

GlobalCacheStateConfig.cacheDataStorage = new BrowserStorageCacheDataStorage(window.localStorage);

SessionStorage data storage

import { GlobalCacheStateConfig, BrowserStorageCacheDataStorage } from '@coool/cachestate';

GlobalCacheStateConfig.cacheDataStorage = new BrowserStorageCacheDataStorage(window.sessionStorage);

Custom cache data storage

import { CacheDataStorage, GlobalCacheStateConfig } from '@coool/cachestate';

class MyCacheDataStorage implements CacheDataStorage {
  // Implement CacheDataStorage interface to store, retrieve and delete cache data
}

GlobalCacheStateConfig.cacheDataStorage = new MyCacheDataStorage(window.localStorage);

API

CacheState configuration

PropertyDescriptionDefaultRequired
cacheKey.generatorGenerates the cache keyCombination of cache key prefix and suffix (combination of class, method name and a hash of the function's arguments)false
cacheKey.prefixGeneratorPrefix for the cache keyCombination of class and method namefalse
cacheKey.suffixGeneratorSuffix for the cache keyA hash of the function's argumentsfalse
cacheDataStorageA storage where the cache data is storedStore cached values locallyfalse
maxAgeMSMax age of cache in milliseconds60000 (1 minute)false
updatedObservableWhen emits the cache is invalidated and updated. If CacheKey is passed then only that cache otherwise all related cache.undefinedfalse
updatedObservableKeyWhen emits the cache is invalidated and updated. If CacheKey is passed then only that cache otherwise all related cache.undefinedfalse
updateOnlySpecificWhen updated notifier fires only update if specific cache key is defined.falsefalse
invalidatedObservableWhen emits the cache is invalidated. If CacheKey is passed then only that cache otherwise all related cache.undefinedfalse
invalidatedObservableKeyWhen emits the cache is invalidated. If CacheKey is passed then only that cache otherwise all related cache.undefinedfalse
invalidateOnlySpecificWhen invalidated notifier fires only update if specific cache key is defined.falsefalse
timestampProviderProvides current timestamp, useful for testingfalse

CacheStateUpdater configuration

PropertyDescriptionDefaultRequired
updatedNotifierWhen emits the cache is invalidated and updated. If CacheKey is passed then only that cache otherwise all related cache.undefinedtrue (OR use updatedNotifierKey)
updatedNotifierKeyGlobal key identifying updatedNotifierundefinedtrue (OR use updatedNotifier)
cacheKeyGeneratorGenerates the cache key for the updated notifierundefinedfalse

CacheStateInvalidator configuration

PropertyDescriptionDefaultRequired
invalidatedNotifierWhen emits the cache is invalidated. If CacheKey is passed then only that cache otherwise all related cache.undefinedtrue (OR use invalidatedNotifierKey)
invalidatedNotifierKeyGlobal key identifying invalidatedNotifier.undefinedtrue (OR use invalidatedNotifier)
cacheKeyGeneratorGenerates the cache key for the updated notifierundefinedfalse

Global configuration

PropertyDescriptionDefaultRequired
cacheDataStorageA storage where the cache data is storedStore cached values locallyfalse
maxAgeMSMax age of cache in milliseconds60000 (1 minute)false
timestampProviderProvides current timestamp, useful for testingfalse

Inspiration

This project is inspired by ts-cacheable

1.4.1

4 months ago

1.4.0

4 months ago

1.3.1

4 months ago

1.2.0

5 months ago

1.3.0

4 months ago

1.2.1

5 months ago

1.1.0

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago