1.6.3 • Published 7 months ago

@knfs-tech/bamimi-cache v1.6.3

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

About BAMIMI Cache

BAMIMI Cache is a lightweight, file-based caching library designed for Node.js applications. It provides flexible caching with optional compression, expiration handling, and keyword search capabilities. The library is easy to use and integrates seamlessly into modern applications.


Installation

Install BAMIMI Cache via npm or yarn:

npm i @knfs-tech/bamimi-cache
# OR
yarn add @knfs-tech/bamimi-cache

Features

  • File-based Caching: Store data directly on the file system for fast and efficient access.
  • Compression: Reduce file sizes with Snappy compression.
  • Expiration Handling: Automatically delete cached entries after a specified duration.
  • Keyword Search: Search through cached entries using keywords with AND/OR logic.
  • Logging: Enable optional logging for debugging and monitoring.
  • Flexible Configuration: Set up default settings for expiration, compression, and cache directory.

Basic Usage

Example

const CacheFile = require('@knfs-tech/bamimi-cache');

// Initialize the cache system
const cache = new CacheFile({
  folder: './my-cache', // Custom cache folder
  autoCompress: true,   // Automatically compress data
  log: true,            // Enable logging
});
cache.setup()

// Store data in the cache
await cache.set('user:123', JSON.stringify({ name: 'John Doe' }), { expire: 60000 });

// Retrieve data from the cache
const userData = await cache.get('user:123');
console.log(JSON.parse(userData));

// Check if a key exists
const exists = await cache.exist('user:123');
console.log(exists ? 'Key exists' : 'Key does not exist');

// Delete a cache entry
await cache.del('user:123');

API Reference

Constructor

new CacheFile(config)

Creates a new instance of BAMIMI Cache.

ParameterTypeDefaultDescriptionSupport Version
config.folderStringprojectPath + /cacheDirectory to store cached files.>= 1.0.2
config.expireNumber0Default expiration time in milliseconds, (0 is not expire)>= 1.0.2
config.autoCompressBooleanfalseEnable auto-compression for cached content.>= 1.0.2
config.logBooleanfalseEnable or disable logging.>= 1.0.2
config.peakDurationNumber3000Allows peak time to use cache to store the results returned when getting, to increase query speed. If peakDuration is 0, it means it is not used.>= 1.1.3
config.maxSizeNumber0Default max size of cache content in bytes, (0 is not verify).>= 1.2.9
config.logHandleNumberuse console.logFunction handle log>= 1.3.0 (Latest)
config.errorHandleNumberuse throw new ErrorFunction handle error.>= 1.3.0

Methods

setup()

Initialize configuration files

set(key, content, options)

Stores data in the cache.

ParameterTypeDescription
keyStringUnique identifier for the cache entry.
contentStringContent to cache.
options.compressBooleanEnable compression for this entry.
options.expireNumberExpiration time in milliseconds.
options.searchArrayKeywords to enable search for this entry.

get(key)

Retrieves cached content by its key.

ParameterTypeDescription
keyStringUnique identifier for the cache.

Returns: Promise<String> - Cached content.


exist(key)

Checks if a key exists in the cache.

ParameterTypeDescription
keyStringUnique identifier for the cache.

Returns: Boolean - true if the key exists, otherwise false.


del(key)

Deletes a cached entry by its key.

ParameterTypeDescription
keyStringUnique identifier for the cache.

search(keywords, logic)

Searches for cached entries based on keywords.

ParameterTypeDefaultDescription
keywordsArray[]Keywords to search for.
logicString (AND/OR)ANDLogic to apply during the search.

Returns: Array<String> - Matching cache keys.


publish(key, message) (>= 1.5.1)

Publish event

ParameterTypeDefaultDescription
keyStringKey or event.
messageanyMessage publish to event.

subscribe(key, listener) (>= 1.5.1)

Subscribe event

ParameterTypeDefaultDescription
keyStringKey or event.
logicfunction(message)Listen event publish.

Ex:

const storage = new CacheFile(configDefault)
storage.setup()

storage.subscribe("event-1", async (message) => {
		console.log("listener 1", message)
})

storage.publish("event-1", contentNumber)

Configuration Example

To customize the cache configuration:

const cache = new CacheFile({
  folder: './cache-directory',
  expire: 60000,         // Default expiration time: 60 seconds
  autoCompress: true,    // Enable automatic compression
  log: true,             // Enable logging
});

Advanced Features

Compression

Uses Snappy for fast and lightweight compression. To enable compression:

await cache.set('key', 'content', { compress: true });

Expiration Handling

Automatically removes expired entries:

await cache.set('tempKey', 'tempData', { expire: 5000 }); // Expires in 5 seconds

Contributions

We welcome contributions! Please create a pull request or submit issues on GitHub.


License

BAMIMI Cache is open-source software licensed under the MIT License.

1.6.3

7 months ago

1.6.2

7 months ago

1.5.2

9 months ago

1.5.1

10 months ago

1.3.0

10 months ago

1.2.9

10 months ago

1.2.8

10 months ago

1.2.6

10 months ago

1.2.5

10 months ago

1.2.3

10 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.1.5

10 months ago

1.1.3

10 months ago

1.0.2

10 months ago