Project

bx-couchcache

An open-source Couchbase cache provider for BoxLang. It covers caching, session storage and bx:lock distributed locking, and boxlang-express accepts it as a durable store for sessions, rate limiting and cluster mode. It doesn't do vector memory: for that, use Ortus's bx-couchbase, which needs a BoxLang+ subscription.

A named cache

Declare a CouchCache provider under caches in boxlang.json and use it like any other BoxLang cache. Couchbase handles expiry, so there is no reaper task and no eviction.

Sessions

Set this.sessionStorage in Application.bx, or pass the cache to boxExpressSession(). requireDurable: true passes, with no "in-memory store" warning.

Distributed locks

bx:lock with a cacheName excludes other instances, not just other threads. Locks are exclusive, not reentrant, and expire by themselves if the holder dies.

No index needed

Key listings, sizes and clears use KV range scans, so there is no N1QL index to create. The Couchbase Java SDK is bundled in the module jar.

Install

bashinstall-bx-module bx-couchcache

Or box install bx-couchcache. Requires BoxLang 1.18+ and Couchbase Server 7.6+.

Configure

In boxlang.json:

json"caches": {
	"couch": {
		"provider": "CouchCache",
		"properties": {
			"connectionString": "couchbase://localhost",
			"username": "${COUCHBASE_USER}",
			"password": "${COUCHBASE_PASSWORD}",
			"bucket": "cache",
			"keyPrefix": "couch:",
			"defaultTimeout": 3600
		}
	},
	"couchSessions": {
		"provider": "CouchCache",
		"properties": {
			"connectionString": "couchbase://localhost",
			"username": "${COUCHBASE_USER}",
			"password": "${COUCHBASE_PASSWORD}",
			"bucket": "cache",
			"collection": "sessions",
			"resetTimeoutOnAccess": true
		}
	}
}

connectionString, username, password and bucket are required. The rest are optional: scope and collection (_default), keyPrefix (the cache name and a colon), connectTimeout (10 seconds), defaultTimeout (3600 seconds; 0 never expires) and resetTimeoutOnAccess (false).

Caches with the same connection string, credentials and bucket share one Couchbase connection. Don't give two caches overlapping prefixes such as app: and app:cart:: the shorter one's getKeys and clearAll would include the longer one's entries.

Sessions

In Application.bx, set this.sessionStorage = "couchSessions". The cache must already exist when the application starts. The BoxLang web runtime writes session changes back at the end of each request; a plain scripting context only stores the session when it is created.

Locks

javascriptbx:lock name="checkout" cacheName="couch" timeout=5 {
	// one instance at a time
}

Locks are exclusive only: the runtime rejects readonly locks when a cacheName is given. The runtime sets the lock's expiry to timeout + 5 seconds and never renews it, so a body that runs longer than that loses the lock. timeout=0 makes a single attempt instead of waiting forever.

With boxlang-express

boxlang-express sessions go through its session middleware and a named cache. Point it at a CouchCache cache:

javascriptapp.use( boxExpressSession( {
	cache: "couchSessions",
	requireDurable: true,
	resave: false,
	saveUninitialized: false
} ) )

boxlang-express sets the document's expiry to the session's maxAge on every write, so leave resetTimeoutOnAccess off for this cache. The same provider works for rate limiting and cluster mode:

javascriptapp.use( boxExpressRateLimit( { max: 100, windowMs: 60000, cache: "couch", requireDurable: true } ) )
app.set( "cluster", { enabled: true, name: "ws://10.0.1.4:3000", cacheProvider: "couch" } )

Rate-limit counters are a read followed by a write, so two instances counting the same client at the same instant can lose a hit. That is how the boxlang-express cache store works on any cache.

When Couchbase is unreachable

  • Reads, writes, lookups, clears and lock attempts throw after the SDK's KV timeout (2.5 seconds by default). The provider retries nothing and serves nothing from memory.
  • A write or clear that timed out was still sent, and Couchbase can apply it once it is reachable again. Treat a timed-out write as "maybe".
  • The cache recovers by itself when Couchbase comes back; no restart is needed.
  • A cache that can't reach its bucket at startup throws after connectTimeout and isn't registered.
  • boxlang-express sessions with the default fallback: true keep working from per-process memory during the outage and go back to Couchbase afterwards.

Differences from BoxCacheProvider

  • maxObjects is ignored and lastAccessTimeout isn't enforced. Use resetTimeoutOnAccess for sliding expiry.
  • Per-entry hits and lastAccessed aren't saved, because saving them would turn every read into a write. Cache-level stats still work, per node.
  • Keys are lowercased: BoxLang keys are case-insensitive and Couchbase IDs aren't.
  • getKeys, getSize and clearAll scan every document under the prefix, which costs O(n).
  • Values are serialized Java objects, not JSON, so only BoxLang can read them.
  • getOrSet only locks within one node. Two nodes can both compute a cold key.

Not verified

  • The boxlang-express clustered scheduler and STOMP relay on this provider
  • Couchbase Enterprise Edition, multi-node clusters and node failover
  • Behaviour under load

Tests

Integration tests start Couchbase Server with Testcontainers, so they need Docker. The suite passes against Couchbase Server Community 8.0.2 and 7.6.2, including two boxlang-express servers in separate JVMs sharing sessions, rate limits, cluster membership and locks:

bash./gradlew downloadBoxLang
./gradlew build
./gradlew test