Skip to content
Jean Luis Urena edited this page Jan 9, 2025 · 5 revisions

Getting Started

Installation

Install the gem using the following command:

gem install active_cached_resource

Compatibility

ActiveCachedResource supports the following ActiveSupport (right) and Ruby (down) version combinations:

🛤️ 6.1 🛤️ 7.0 🛤️ 7.1
💎 3.1
💎 3.2

Configuration

Set up a Cached ActiveResource model:

class MyActiveResource < ActiveResource::Base
  cached_resource(options)
end

Options

cached_resource accepts the following options as a hash:

Option Description Default
:enabled Enables or disables caching. true
:cache The cache store that ActiveCacheResource should use. Must be a CachingStrategies::Base instance. Required if cache_store and cache_strategy aren't provided.
:cache_store The cache store to be used if cache isn't provided. Required with cache_strategy if cache isn't provided.*
:cache_strategy The cache strategy to be used. One of :active_record_sql or :active_support_cache. Required with cache_store if cache isn't provided.
:cache_key_prefix A prefix to be added to the cache keys. Required
:logger The logger to which CachedResource messages should be written. ActiveCachedResource::Logger
:ttl The time in seconds until the cache should expire. 86400

For example:

# Using a custom cache store and cache strategy
class MyResource < ActiveResource::Base
  cached_resource(
    enabled: true,
    cache_store: Rails.cache,
    cache_strategy: :active_support_cache,
    cache_key_prefix: "my_resource",
    logger: Rails.logger,
    ttl: 3600
  )
end

# Using a custom cache instance that inherits from CachingStrategies::Base
class MyOtherResource < ActiveResource::Base
  custom_cache = MyCustomCacheStore.new
  cached_resource(
    cache: custom_cache,
    cache_key_prefix: "other_resource"
  )
end