ESC
Type to search...
S
Soli Docs

Cache Functions

In-memory caching with TTL support. Use static methods on the Cache class for a clean API.

Cache Static Methods

Call methods directly on the Cache class without instantiation.

Cache.set(key, value, ttl_seconds?)

Stores a value in the cache.

  • key (String) - Cache key
  • value (Any) - Value to cache (JSON serialized)
  • ttl_seconds (Int, optional) - Time to live (default: 3600)

Returns: null

Cache.set("user:123", { "name": "Alice" })
Cache.set("session", data, 1800)  # 30 minute TTL

Cache.get(key)

Retrieves a value from the cache.

Returns: Any|null - Cached value or null if not found/expired

let user = Cache.get("user:123")
if user != null {
    println("Cached user: " + user["name"])
}

Cache.delete(key)

Removes a value from the cache.

Returns: Bool - true if key was removed

Cache.has(key)

Checks if a key exists and is not expired.

Returns: Bool

Cache.clear()

Removes all entries from the cache.

Returns: null

Cache.clear_expired()

Removes only expired entries.

Returns: null

Cache.keys()

Returns all valid (non-expired) cache keys.

Returns: Array

Cache.size()

Returns the number of entries in the cache.

Returns: Int

Cache.ttl(key)

Gets the remaining TTL for a key in seconds.

Returns: Int|null

Cache.touch(key, ttl)

Extends or sets the TTL for an existing key.

Returns: Bool

Cache Instance

Create a Cache instance using cache(). All instances share the same underlying store.

let c = cache()
c.set("key", "value")
let value = c.get("key")

Global Functions (Backward Compatibility)

Global cache functions are also available and share the same store.

cache_set(key, value, ttl?)

Store a value in cache

cache_get(key)

Retrieve a value from cache

cache_delete(key)

Remove a value from cache

cache_has(key)

Check if key exists

cache_clear()

Clear all entries

cache_config(ttl?, max_size?)

Configure cache defaults

Complete Example

# Using static methods (recommended)
Cache.set("user:123", { "name": "Alice", "email": "[email protected]" })
Cache.set("session", session_data, 1800)  # 30 minute TTL

# Retrieve data
let user = Cache.get("user:123")
if user != null {
    println("Cached user: " + user["name"])
}

# Check existence
if Cache.has("user:123") {
    println("User still cached")
}

# Update TTL
Cache.touch("user:123", 3600)

# List keys
let keys = Cache.keys()

# Check size
let count = Cache.size()

# Clear cache
Cache.clear()

# Or use a Cache instance
let c = cache()
c.set("instance_key", "value")

# Global functions also work (share the same store)
cache_set("global_key", "value")
let value = cache_get("global_key")

Practical Example: Database Query Caching

def get_user_cached(user_id)
    let cache_key = "user:" + str(user_id)
    let cached = Cache.get(cache_key)

    if cached != null {
        println("Cache hit!")
        return cached
    }

    println("Cache miss - querying database...")
    let user = db.query("SELECT * FROM users WHERE id = ?", [user_id])

    # Cache for 5 minutes
    Cache.set(cache_key, user, 300)

    user
end

Configuration

Configure global cache settings:

  • Default TTL: 3600 seconds (1 hour)
  • Max Size: 10000 entries
# Configure cache
cache_config(1800, 5000)  # 30 min TTL, 5000 max entries