LogoLogo
  • Introduction
  • Features
  • Getting Started
    • Cortex User Guide
      • Organizations
      • Log in
      • Generate an API key
      • Make your first API request
      • Configure the org settings
      • Set up a data model
        • Create custom objects
        • Add custom properties
      • One-to-many relationships
      • Read and write data
      • Making a Request
      • Handling responses
      • Authentication
      • Two-factor authentication
      • Set third-party cookies
      • Connections
      • Upload files
      • Cortex iOS
      • Get started with Swift
      • Cortex service accounts
      • Cortex developer tools
      • Automated Account Notifications
  • Cortex API
    • Overview
    • Objects
      • Objects Overview
      • Organization
      • Account
      • Connections
      • Notifications
      • Stats
      • Logs
      • Export
      • Events
      • Room
    • Object Definition
      • Object Properties
        • Any
        • Binary
        • Boolean
        • Date
        • Document
        • File
        • Geometry
        • List
        • Number
        • ObjectId
        • Reference
        • String
    • Object Types
    • Access Control
    • Querying
      • Query Operators
      • Property Selection
      • Property Access
    • Aggregating
      • Aggregation Operators
    • Scripting
      • Script Types
      • Script Limits
      • ObjectId
      • CortexObject
        • Accounts
        • Views
      • Cursors
      • Operations
      • Script Modules
        • API Module
        • Base64 Module
        • Cache Module
        • Connections Module
        • Console Module
        • Consts Module
        • Counters Module
        • Crypto Module
        • HTTP Module
        • Logger Module
        • Notifications Module
        • Request Module
        • Response Module
        • SAML Module
        • Schemas Modules
        • Script Module
        • Session Module
        • Util.id Module
        • Util.ip Module
        • Util.paths Module
        • XML Module
        • Developer
        • Config
        • Renderer
        • SFTP
        • FTP
        • DB
          • Cursors
          • Driver
      • Static Methods
        • Accounts
        • Views
        • Cursors
      • Audit
      • Environments
      • HTTP Driver
      • Notifications
        • Firebase Cloud Messaging (FCM)
        • Tencent Push Notification Service Configuration
      • Televisit
      • Transforms
      • Localization
      • Available Javascript Libraries
    • Decorators
      • Runtime
        • Acl
        • As
        • Log
        • Profile
      • Static
        • Env
        • Job
        • Object
        • On
        • Policy
        • Route
        • Transform
        • Trigger
    • Expressions
      • Primer
      • Pipelines
      • Operators
      • Accumulators
      • Variables
      • Conditionals
      • Transforms
      • Triggers
      • On
      • Events
    • Faults
      • Fault Reference
  • Releases
    • Cortex Release Notes
      • Cortex API 2.28.3 (R3.4.6)
      • Cortex API 2.28.1 (R3.4.3)
      • Cortex API 2.27.2 (R3.4.1)
      • Cortex API 2.27.1 (R3.3.5)
      • SQL DB Connector 1.3.4 (R3.3.3)
      • Cortex API 2.26.2 (R3.3.1)
      • Cortex API 2.26.1 (R3.2.2)
      • Cortex API 2.26.0 (R3.2.1)
      • SQL DB Connector 1.3.3
      • Cortex API 2.25.0 (R3.1.1)
      • SQL DB Connector 1.3.2 (R3.1.0)
      • Cortex API 2.24.2 (R3.0.2)
      • SQL DB Connector 1.3.1 (R3.0.0)
      • Cortex API 2.24.1 (R2.3.3)
      • Cortex API 2.24.0 (R2.3.2)
      • SQL DB Connector 1.3.0 (R2.3.0)
      • Cortex API 2.23.0 (R2.2.4)
      • SQL DB Connector 1.2.0 (R2.2.0)
      • Cortex API 2.22.2 (R2.1.2)
      • Cortex API 2.22.1 and SQL DB Connector 1.1.1 (R2.0.1)
      • Cortex API 2.22.0
      • Cortex API 2.21.3
      • Cortex API 2.21.2
      • Cortex Web 4.16.0
      • Cortex Web 4.15.1
      • Cortex API 2.20.1
      • Cortex Web 4.14.0
      • Cortex Renderer 1.3.3
      • Cortex API 2.19.4
      • Cortex API 2.19.3 and Cortex Web 4.13.1
      • Cortex Renderer 1.3.2
      • Cortex API 2.19.1
      • Cortex API 2.18.0
      • Cortex API 2.17.6
      • Cortex API 2.17.5
      • Cortex API 2.17.4
      • Cortex API 2.17.3
      • Cortex API 2.17.2
      • Cortex API 2.17.1
      • Cortex API 2.16.0
      • Cortex API 2.15.9
      • Cortex API 2.15.8-1
      • Cortex 2.15.8
      • Cortex API 2.18.1
      • Cortex API 2.16.1
      • Cortex Renderer 1.3.1
      • Cortex Renderer 1.3.0
      • Cortex Renderer 1.2.2
      • Cortex Renderer 1.2.1
      • Cortex Renderer 1.2.0
    • Third-Party License Attribution

© 2025 Medable, Inc. All rights reserved.

On this page
  • Methods
  • cas(key, chk, val, ttl = null)
  • clear(search='')
  • count(search='')
  • del(key)
  • find(search='', skip=0, limit=100)
  • get(key)
  • has(key)
  • list(search='', skip=0, limit=100)
  • set(key, val, ttl = null)
  • Examples

Was this helpful?

  1. Cortex API
  2. Scripting
  3. Script Modules

Cache Module

Last updated 3 years ago

Was this helpful?

General purpose key/value storage with searchable keys.

The cache module serves as general purpose key/value storage, accessible from scripts for all principals and for developers from the REST API, using /cache routes.

🚧No ACL

There are no access controls surrounding cache data. Users should keep PHI and PII out of the cache or otherwise ensure only valid principals are pulling out entries.

📘Metered

Cache storage is metered at the same rate as Documents and Media storage.

Import

import cache from 'cache';

Methods

cas(key, chk, val, ttl = null)

Atomic compare and swap.

Arguments

  • key (String) The cache key to compare and swap.

  • chk (Object) The value to compare. the update only occurs if chk matches the value in the cache.

  • val (Object) The value to set.

  • ttl (Number) Optional time-to-live for cache entry, in seconds.

Returns

  • updated (Boolean) true if the value was set, or false if the cache value did not initially equal chk.

clear(search='')

Clears cache values starting with search (or the entire cache).

Arguments

  • search (String) Any keys starting with search are removed. To avoid inadvertently clearing keys, adopt a key naming strategy that guarantees key domains remain segregated. For example, prefix all cache keys involving single sign-on with sso.. Leaving this argument blank will delete the entire cache.

Returns

  • removed (Number) the number of keys removed.

count(search='')

Returns a count of cache entries starting with search (or the entire cache).

Arguments

  • search (String) Any keys starting with search are counted.

Returns

  • count (Number) the number of matching keys.

del(key)

Removes the cache entries with a key matching key.

Arguments

  • key (String) The key to remove.

Returns

  • removed (Boolean) true if the value was removed.

find(search='', skip=0, limit=100)

Returns an array of matching entry key/value pairs, sorted by key.

Arguments

  • search (String) The key filter. All entries starting with search are returned.

  • skip (Number) The number of entries to skip. Useful for paging.

  • limit (Number) The maximum number of entries to return, from 1 to 1000.

Returns

  • entries (Object[]) A list of matching entries.

    • key (String) The entry key.

    • val (String) The entry value.

get(key)

Returns the value of a cache entry matching key.

Arguments

  • key (String) The key to match.

Returns

  • value (Object) Returns the value of the entry, or null if not found.

has(key)

Returns true if a cache entry matching key exists. For use in cases where null is a valid entry value.

Arguments

  • key (String) The key to match.

Returns

  • exists (Boolean) Returns true if the entry exists in the cache.

list(search='', skip=0, limit=100)

Returns an array of matching entry metadata, sorted by key. List results may include recently expired keys.

Arguments

  • search (String) The key filter. All entries starting with search are returned.

  • skip (Number) The number of entries to skip. Useful for paging.

  • limit (Number) The maximum number of entries to return, from 1 to 1000.

Returns

  • entries (Object[]) A list of matching entry metadata.

    • key (String) The entry key.

    • created (Date) The time the entry was initially created.

    • sz (Number) The total size of the cache entry, including overhead.

    • exp (Date) For keys with a ttl, the time the entry expires.

set(key, val, ttl = null)

Set or update a cache entry.

Arguments

  • key (String) The key to set.

  • val (Object) The value to set.

  • ttl (Number) Optional time-to-live for cache entry, in seconds.

Examples

slow process ttl caching

import cache from 'cache';
import http from 'http';

let data = cache.get('sites:example.com');
if (!data) {
  data = http.get('https://www.example.com/').body;
  cache.set('sites:example.com', data, 86400); // cache for a day
}
return data;

Atomic increment

import cache from 'cache';

while (1) {
    const curr = cache.get('increment.foo'),
          next = curr + 1;
    if (cache.cas('increment.foo', curr, next)) {
        return next;
    }
}
cas(key, chk, val, ttl=null)
clear(search='')
count(search='')
del(key)
find(search='', skip=0, limit=100)
get(key)
has(key)
list(search='', skip=0, limit=100)
set(key, val, ttl=null)