Cache

The cache module provides flexible caching with multiple backend support.

Cache Stores

MemoryStore

In-process memory cache.

Azu.configure do |config|
  config.cache = Azu::Cache::MemoryStore.new
end

Options:

  • max_size : Int32 - Maximum entries (default: 10000)

  • default_ttl : Time::Span - Default expiration (default: 1.hour)

RedisStore

Redis-backed distributed cache.

Azu.configure do |config|
  config.cache = Azu::Cache::RedisStore.new(
    url: ENV["REDIS_URL"]
  )
end

Options:

  • url : String - Redis connection URL

  • pool_size : Int32 - Connection pool size

  • namespace : String? - Key prefix

  • default_ttl : Time::Span - Default expiration

Cache Methods

set

Store a value.

Parameters:

  • key : String - Cache key

  • value : String - Value to store

  • expires_in : Time::Span? - Optional TTL

get

Retrieve a value.

Parameters:

  • key : String - Cache key

Returns: String? - Cached value or nil

fetch

Get or compute a value.

Parameters:

  • key : String - Cache key

  • expires_in : Time::Span? - Optional TTL

  • &block - Block to compute value if missing

Returns: String - Cached or computed value

delete

Remove a value.

Parameters:

  • key : String - Cache key

exists?

Check if key exists.

Parameters:

  • key : String - Cache key

Returns: Bool

clear

Remove all cached values.

increment

Increment a numeric value.

Parameters:

  • key : String - Cache key

  • by : Int32 - Increment amount (default: 1)

Returns: Int32 - New value

decrement

Decrement a numeric value.

Parameters:

  • key : String - Cache key

  • by : Int32 - Decrement amount (default: 1)

Returns: Int32 - New value

expire

Set expiration on existing key.

Parameters:

  • key : String - Cache key

  • ttl : Time::Span - Time to live

RedisStore-Specific Methods

delete_matched

Delete keys matching a pattern.

Parameters:

  • pattern : String - Glob pattern

ttl

Get remaining time to live.

Parameters:

  • key : String - Cache key

Returns: Time::Span? - Remaining TTL or nil

Cache Patterns

Cache-Aside

Write-Through

Cache Stampede Prevention

Configuration Example

See Also

Last updated

Was this helpful?