Coder Social home page Coder Social logo

es-cache's Introduction

es-cache

A simple in-memory cache on ecmascript 6 and typescript.

Installation

npm install es-cache

Usage

var Cache = require('es-cache');
var cache = new Cache();

// now just use the cache

cache.put('foo', 'bar');
console.log(cache.get('foo'))

// that wasn't too interesting, here's the good part

cache.put('houdini', 'disappear', 100) // Time in ms
console.log('Houdini will now ' + cache.get('houdini'));

setTimeout(function() {
  console.log('Houdini is ' + cache.get('houdini'));
}, 200);

which should print

bar
Houdini will now disappear
Houdini is null

API

put = function(key, value, time, callback)

  • Simply stores a value
  • If value is a function then function returned value is cached.
  • If time isn't passed in, it is stored forever
  • If time is passed then value is refreshed after the specified time using the specified function. Will actually remove the value in the specified time in ms (via setTimeout)
  • executes the callback function when value is removed.
  • Returns the cached value

get = function(key)

  • Retrieves a value for a given key
  • If value isn't cached, returns null

del = function(key)

  • Deletes a key, returns a boolean specifying whether or not the key was deleted

clear = function()

  • Deletes all keys

size = function()

  • Returns the current number of entries in the cache

keys = function()

  • Returns all the cache keys

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Send me a pull request.

es-cache's People

Contributors

nitinet avatar

Stargazers

Jakub Krupka avatar

Watchers

James Cloos avatar  avatar

Forkers

rohankanojia jpzk

es-cache's Issues

Cant use your pkg with tsconfig module targetd to ES6

Hi Nitin,

Nice package for caching functionality. I request to implement one feedback.
My Typescript project has requirement to use module: ES6 in tsconfig file.
Your library is using dynamic module export which is not supported in ES6. Can you please modify that so I can use it in my project?

switch (options.storeType) {
case types.StoreType[types.StoreType.local]:
module = await import('./store/Local.js');
break;

		case types.StoreType[types.StoreType.redis]:
			module = await import('./store/Redis.js');
			break;

		case types.StoreType[types.StoreType.memcache]:
			module = await import('./store/Memcache.js');
			break;
		default:
			module = await import('./store/Local.js');
	}

es-cache requires other dependencies

Hi, great tool, pretty simple and up for the task but when adding es-cache to a node project and running it, I got these errors :

> node_modules/es-cache/dist/store/Memcache.js:15:67: error: Could not resolve "memcached" (mark it as external to exclude it from the bundle, or surround it with try/catch to handle the failure at run-time)
    15 │ ...emcached = await Promise.resolve().then(() => require('memcached'));
       ╵                                                          ~~~~~~~~~~~

> node_modules/es-cache/dist/store/Redis.js:18:63: error: Could not resolve "redis" (use "./redis" to reference the file "node_modules/es-cache/dist/store/redis.js")
    18 │ ...   let redis = await Promise.resolve().then(() => require('redis'));
       ╵                                                              ~~~~~~~

Adding memcached and redis to my project solved the issue but maybe you should mention it in the readme on include theses dependencies in your project ?

Also, it's a bit of a shame I have to add memcached and redis since I only use the local store but it's not that important to me

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.