Class: ApiWrapper::Cache::CachePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/api_wrapper/cache/cache_policy.rb

Overview

CachePolicy manages caching behavior, including cache storage and time-to-live (TTL) settings.

It allows setting global TTLs, custom TTLs for specific endpoints, and controlling which endpoints should not use the cache.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_store, global_ttl = 300) ⇒ CachePolicy

Initializes the CachePolicy with a cache store and global TTL.

Parameters:

  • cache_store (CacheStore, RedisCacheStore)

    The cache store to use for caching.

  • global_ttl (Integer) (defaults to: 300)

    The default TTL (in seconds) for caching.



18
19
20
21
22
23
# File 'lib/api_wrapper/cache/cache_policy.rb', line 18

def initialize(cache_store, global_ttl = 300)
  @cache_store = cache_store
  @global_ttl = global_ttl
  @custom_ttls = {}
  @no_cache_endpoints = []
end

Instance Attribute Details

#cache_storeCacheStore (readonly)

The cache store used for storing cached data.

Returns:

  • (CacheStore)

    the current value of cache_store



11
12
13
# File 'lib/api_wrapper/cache/cache_policy.rb', line 11

def cache_store
  @cache_store
end

Instance Method Details

#add_custom_ttl(endpoint, ttl = 300) ⇒ Object

Adds a custom TTL for a specific endpoint.

Parameters:

  • endpoint (String)

    The endpoint to apply a custom TTL to.

  • ttl (Integer) (defaults to: 300)

    The custom TTL value in seconds.



36
37
38
# File 'lib/api_wrapper/cache/cache_policy.rb', line 36

def add_custom_ttl(endpoint, ttl = 300)
  @custom_ttls[endpoint] = ttl
end

#add_no_cache_endpoint(endpoint) ⇒ Object

Adds an endpoint that should bypass the cache.

Parameters:

  • endpoint (String)

    The endpoint to exclude from caching.



28
29
30
# File 'lib/api_wrapper/cache/cache_policy.rb', line 28

def add_no_cache_endpoint(endpoint)
  @no_cache_endpoints << endpoint
end

#fetch(endpoint, force_refresh: false) { ... } ⇒ Object

Fetches the data for the given endpoint, using cache if applicable.

Parameters:

  • endpoint (String)

    The endpoint to fetch data for.

  • force_refresh (Boolean) (defaults to: false)

    Whether to force refresh the data, bypassing the cache.

Yields:

  • The block that fetches fresh data if cache is not used or is stale.

Returns:

  • (Object)

    The data fetched from cache or fresh data.



62
63
64
65
66
67
68
# File 'lib/api_wrapper/cache/cache_policy.rb', line 62

def fetch(endpoint, force_refresh: false, &block)
  if force_refresh || !use_cache?(endpoint)
    fetch_fresh_data(endpoint, &block)
  else
    fetch_cached_or_fresh_data(endpoint, &block)
  end
end

#ttl_for(endpoint) ⇒ Integer

Returns the TTL for a specific endpoint. Defaults to the global TTL if no custom TTL is set.

Parameters:

  • endpoint (String)

    The endpoint to fetch the TTL for.

Returns:

  • (Integer)

    The TTL in seconds.



44
45
46
# File 'lib/api_wrapper/cache/cache_policy.rb', line 44

def ttl_for(endpoint)
  @custom_ttls.fetch(endpoint, @global_ttl)
end

#use_cache?(endpoint) ⇒ Boolean

Determines if caching should be used for the given endpoint.

Parameters:

  • endpoint (String)

    The endpoint to check.

Returns:

  • (Boolean)

    True if caching is enabled for the endpoint, false otherwise.



52
53
54
# File 'lib/api_wrapper/cache/cache_policy.rb', line 52

def use_cache?(endpoint)
  !@no_cache_endpoints.include?(endpoint)
end