Modules@robinpath/cache
cache

@robinpath/cache

0.1.2Node.jsPublic

In-memory key-value cache with optional TTL expiration for temporary data storage

Cache

In-memory key-value cache with optional TTL expiration for temporary data storage

Package: @robinpath/cache | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the cache module when you need to:

  • Retrieve a value from the cache by key -- Use cache.get to perform this operation
  • Check if a non-expired key exists in the cache -- Use cache.has to perform this operation
  • Remove a key from the cache -- Use cache.delete to perform this operation
  • Remove all entries from the cache -- Use cache.clear to perform this operation
  • Get all non-expired keys in the cache -- Use cache.keys to perform this operation

Quick Reference

FunctionDescriptionReturns
setStore a value in the cache with an optional TTLTrue if the value was stored
getRetrieve a value from the cache by keyThe cached value, the default value, or null if not found
hasCheck if a non-expired key exists in the cacheTrue if the key exists and has not expired
deleteRemove a key from the cacheTrue if the key was deleted
clearRemove all entries from the cacheTrue if the cache was cleared
keysGet all non-expired keys in the cacheArray of all non-expired cache keys
valuesGet all non-expired values in the cacheArray of all non-expired cache values
sizeGet the number of non-expired entries in the cacheCount of non-expired cache entries
ttlGet the remaining time-to-live for a cache keyRemaining TTL in seconds, -1 if no expiry, or null if key does not exist
setManyStore multiple key-value pairs in the cache at onceNumber of entries that were stored
getManyRetrieve multiple values from the cache by keysObject of key-value pairs for found non-expired keys
deleteManyRemove multiple keys from the cache at onceNumber of entries that were deleted

Functions

set

Store a value in the cache with an optional TTL

Module: cache | Returns: boolean -- True if the value was stored

cache.set "user:1" "Alice" 60
ParameterTypeRequiredDescription
keystringYesCache key to store the value under
valueanyYesValue to store in the cache
ttlnumberNoTime-to-live in seconds (omit or null for no expiry)

get

Retrieve a value from the cache by key

Module: cache | Returns: any -- The cached value, the default value, or null if not found

cache.get "user:1" "unknown"
ParameterTypeRequiredDescription
keystringYesCache key to look up
defaultValueanyNoValue to return if the key is not found or expired

has

Check if a non-expired key exists in the cache

Module: cache | Returns: boolean -- True if the key exists and has not expired

cache.has "user:1"
ParameterTypeRequiredDescription
keystringYesCache key to check

delete

Remove a key from the cache

Module: cache | Returns: boolean -- True if the key was deleted

cache.delete "user:1"
ParameterTypeRequiredDescription
keystringYesCache key to delete

clear

Remove all entries from the cache

Module: cache | Returns: boolean -- True if the cache was cleared

cache.clear
ParameterTypeRequiredDescription
(none)NoCall with no arguments

keys

Get all non-expired keys in the cache

Module: cache | Returns: array -- Array of all non-expired cache keys

cache.keys
ParameterTypeRequiredDescription
(none)NoCall with no arguments

values

Get all non-expired values in the cache

Module: cache | Returns: array -- Array of all non-expired cache values

cache.values
ParameterTypeRequiredDescription
(none)NoCall with no arguments

size

Get the number of non-expired entries in the cache

Module: cache | Returns: number -- Count of non-expired cache entries

cache.size
ParameterTypeRequiredDescription
(none)NoCall with no arguments

ttl

Get the remaining time-to-live for a cache key

Module: cache | Returns: number -- Remaining TTL in seconds, -1 if no expiry, or null if key does not exist

cache.ttl "user:1"
ParameterTypeRequiredDescription
keystringYesCache key to check TTL for

setMany

Store multiple key-value pairs in the cache at once

Module: cache | Returns: number -- Number of entries that were stored

cache.setMany {"a": 1, "b": 2} 120
ParameterTypeRequiredDescription
entriesobjectYesObject of key-value pairs to store
ttlnumberNoTime-to-live in seconds for all entries (omit or null for no expiry)

getMany

Retrieve multiple values from the cache by keys

Module: cache | Returns: object -- Object of key-value pairs for found non-expired keys

cache.getMany ["a", "b", "c"]
ParameterTypeRequiredDescription
keysarrayYesArray of cache keys to look up

deleteMany

Remove multiple keys from the cache at once

Module: cache | Returns: number -- Number of entries that were deleted

cache.deleteMany ["a", "b"]
ParameterTypeRequiredDescription
keysarrayYesArray of cache keys to delete

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Get and validate result"
do
  set $result as cache.get "user:1" "unknown"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as cache.get "user:1" "unknown"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Cache workflow

Chain multiple cache operations together.

@desc "Get, has, and more"
do
  set $r_get as cache.get "user:1" "unknown"
  set $r_has as cache.has "user:1"
  set $r_delete as cache.delete "user:1"
  print "All operations complete"
enddo

3. Safe get with validation

Check results before proceeding.

@desc "Get and validate result"
do
  set $result as cache.get "user:1" "unknown"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.2latest1 months ago
Install
$ robinpath add @robinpath/cache

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.2
LicenseMIT
Unpacked Size4.7 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops