1.0.1 • Published 7 years ago

image-cache v1.0.1

Weekly downloads
83
License
MIT
Repository
github
Last release
7 years ago

image-cache

Build Status npm version

Powerful image cache for NodeJS

What is image-cache?

image-cache is nodejs module for cache image and serve with base64 format. image-cache using Asynchronous calls for best Performance.

New

Now image-cache using google proxy cache for best caching.

Installation

to install image-cache on your project, you can use NPM and Yarn with the following command,

NPM

npm install --save image-cache 

Yarn

yarn add image-cache

Usage

Start using image-cache

var imageCache = require('image-cache');

API

setOptions

is a function to replace options from default options. this function not returning something. for using this function, add this code after defining all variable.

imageCache.setOptions({
   compressed: false

   // write your custom options here
});

API

KeyData TypeDefault ValueDescription
dirstringpath.join(__dirname, 'cache/')directory root for cached files
compressedbooleantruecompressing cache output with zlib compressing maybe can make your processing cache little bit longer. for example without compressing is 6-7ms when using compressing is 150-185ms, but your cache file is a litle bit smaller than without compressing
extnamestring.cachefile extension for your cache files
googleCachebooleantrueusing google cache proxy

isCached()

Check is your image already cached or not. this function need 2 params.

imageCache.isCached(url, callback);

Params

KeyData Type
urlstring
callbackfunction

Example

imageCache.isCached(url, function(exist) {
   if (exist) {
      // do something with cached image
   }
});

isCachedSync()

Check is your image is cached with Synchronous processing. return as boolean

Example

var exists = imageCache.isCachedSync(url);

API

ParamsData TypeDescription
urlstringurl of your image

getCache()

Get Cached Image

Example

imageCache.getCache(url, function(error, image) {
   console.log(image);

   // do something with image
});

API Callback

KeyData TypeDescription
imageobjectcache data object
image.errorbooleancache data error indicator
image.urlstringimage source url before transform to base64 format
image.hashFilestringfilename cache
image.timestampintegertimestamp when cache created
image.compressedbooleanis that cache compressed
image.datastringbase64 code, ugly text from your beauty images

getCacheSync

Get Cached image with Synchronous processing.

Example

var image = imageCache.getCache('http://domain/path/to/image.png');

API

API return same like .getCache()

setCache()

Set new Cache, write cache files into options.dir Directory. set cache is working with multiple images.

imageCache.setCache(images, callback);

Params

KeyData Type
imagesarraystring
callbackfunction

Example

let images = 'https://eladnava.com/content/images/2015/11/js-6.jpg';
imageCache.setCache(images, function(error) {
   console.log(error);
});

API Callback

KeyData TypeDescription
errorobjectthis error came from fs.writeFile

flushCache()

Delete all cache files on your directory. this code will delete all cache on options.dir with extension name same as options.extname.

Example

imageCache.flushCache(function(error, results) {
   if (error) {
      console.log(error);
   } else {
      console.log(results);
   }
});

API Callback

KeyData TypeDescription
resultsobjectdetails
results.deletednumbertotal of deleted files
results.totalFilesnumbertotal files on directory
results.dirstringDirectory cache images

flushCacheSync()

same like flushCache, but using Synchronous processing.

Example

var results = imageCache.flushCache();

API

same like flushCache()

KeyData TypeDescription
results.errorbooleanerror statement
results.messagestringerror message

fetchImage()

fetchImage is a function to store cache and get cache data in one time. fetchImage using Async processing for best performace. fetchImage() check your cache file first, if your image is not available in cache folder then this function will get image and return your cache data.

fetchImage() using Promise. fetchImage callback is return as Array.

Params

KeyData TypeDescription
imagestring or arrayimage or array of images

Example

var image = "http://path.to/image.jpg";

imageCache.fetchImage(image).then((images) => {
   images.forEach((image) => {
      console.log(image);

      // { ... }
   });
   console.log(images);

   // [ { ... } ]
});

API

KeyData TypeDescription
imagesarraycache data array group
imageobjectcache data object
image.errorbooleancache data error indicator
image.urlstringimage source url before transform to base64 format
image.hashFilestringfilename cache
image.timestampintegertimestamp when cache created
image.compressedbooleanis that cache compressed
image.datastringbase64 code, ugly text from your beauty images
image.cachestringcache status is "MISS" or "HIT"

MISS or HIT

MISS

when your image is not available in the cache folder, image will be grab from image URL then cached on cache folder.

HIT

when your image is available in the cache folder, the image will be grab directly from cache folder.