SIMPLE_CACHE_QUICK

Zero-Configuration Caching for Beginners

v1.0.0 MIT

Overview

SIMPLE_CACHE_QUICK provides the simplest possible caching with the powerful remember pattern - get from cache or compute and store automatically.

For full control including Redis support, use SIMPLE_CACHE directly.

Quick Start

local
    cache: SIMPLE_CACHE_QUICK
    value: detachable STRING
do
    create cache.make  -- default 1000 entries

    -- Basic get/set
    cache.set ("user:123", user_json)
    value := cache.get ("user:123")

    -- With TTL (expires in 1 hour)
    cache.set_for ("session", token, 3600)

    -- The killer feature: get-or-compute
    value := cache.remember ("expensive_key", agent compute_value)
    -- Returns cached value if exists, otherwise computes and caches

    -- With TTL
    value := cache.remember_for ("data", 300, agent fetch_data)

    -- Counters
    cache.increment ("page_views")
    cache.increment_by ("score", 10)

    -- Statistics
    print (cache.stats)  -- "Hits: 42, Misses: 8, Rate: 84%"
end

The Remember Pattern

This is the most useful caching pattern - eliminates boilerplate cache checking:

-- Without remember (verbose)
value := cache.get ("key")
if value = Void then
    value := expensive_computation
    cache.set ("key", value)
end

-- With remember (one-liner)
value := cache.remember ("key", agent expensive_computation)

API Reference

Basic Operations

FeatureDescription
get (key)Get cached value
set (key, value)Store value
set_for (key, value, ttl)Store with TTL in seconds
delete (key)Remove entry
has (key)Check if key exists
clearClear all entries

Remember Pattern

FeatureDescription
remember (key, agent)Get or compute and cache
remember_for (key, ttl, agent)Get or compute with TTL

Counters

FeatureDescription
increment (key)Increment by 1
increment_by (key, n)Increment by n
decrement (key)Decrement by 1

Statistics

FeatureDescription
statsFormatted statistics string
hit_rateCache hit rate (0.0-1.0)
countNumber of cached entries