Git Product home page Git Product logo

async-cache-service's Introduction

AsyncCacheService

The asynchronous cache service is designed to handle the storage and retrieval of data in a non-blocking manner. When data is being loaded into the cache, the service returns a Promise object to the requester. This Promise serves as a placeholder or notification that the data is currently in the process of being fetched.

Once the data loading process is complete and the data is successfully retrieved, the cache service resolves all previously issued Promises associated with that specific data. By resolving the Promises, the cache service effectively provides the updated and complete data to all subscribers or requesters who were waiting for it.

In summary, the asynchronous cache service employs Promises to handle the asynchronous loading of data, allowing subscribers to receive notifications when the data is being fetched, and resolving the Promises with the updated data once it becomes available.

How to use

Initialization

The cache service can handle any type of data, defined on initialization:

// by default - the cache is never expire
const cacheService = new AsyncCacheService<string>();

// with 5min expiration
const cacheService = new AsyncCacheService<string>(300_000);

// as a dependency with 15min expiration
export class SomeDataService {
  constructor(private cacheService = new AsyncCacheService<string>(900_000)) {}
}

Each record in the cache should be linked with uniq key, defined by user.

Data flow

First, "Client" need to check cached data expiration status by calling isExpire method with a key as an argument:

// check record expire
cacheService.isExpired('testRecord1');

Then "Client" marks this record as refreshing, it means that other clients will receive Promise on getItem('testRecord1') method call

// mark record as refreshing, since this moment other clients will receive Promise on getItem('testRecord1') call
cacheService.refreshItem('testRecord1');

Once data has been loaded, "Client" sets cache with the new data. Other clients will get previously obtained Promises resolved with this data.

// update record with new data, since this moment other clients will receive testValue as Promise resolution
cacheService.setItem('testRecord1', 'testValue');

"Client" can flush particular record from cache by calling flushItem method, if there are some other clients awaiting for data, they receive Promise rejection.

// flush or clear a particular record from cache, since this momment other clients will receive Promise rejection (if they previously subscribed to such data)
cacheService.flushItem('testRecord1');

async-cache-service's People

Contributors

frozer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.