Class: ApiWrapper::Cache::CachePolicy
- Inherits:
-
Object
- Object
- ApiWrapper::Cache::CachePolicy
- 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
-
#cache_store ⇒ CacheStore
readonly
The cache store used for storing cached data.
Instance Method Summary collapse
-
#add_custom_ttl(endpoint, ttl = 300) ⇒ Object
Adds a custom TTL for a specific endpoint.
-
#add_no_cache_endpoint(endpoint) ⇒ Object
Adds an endpoint that should bypass the cache.
-
#fetch(endpoint, force_refresh: false) { ... } ⇒ Object
Fetches the data for the given endpoint, using cache if applicable.
-
#initialize(cache_store, global_ttl = 300) ⇒ CachePolicy
constructor
Initializes the CachePolicy with a cache store and global TTL.
-
#ttl_for(endpoint) ⇒ Integer
Returns the TTL for a specific endpoint.
-
#use_cache?(endpoint) ⇒ Boolean
Determines if caching should be used for the given endpoint.
Constructor Details
#initialize(cache_store, global_ttl = 300) ⇒ CachePolicy
Initializes the CachePolicy with a cache store and global TTL.
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_store ⇒ CacheStore (readonly)
The cache store used for storing cached data.
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.
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.
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.
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.
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.
52 53 54 |
# File 'lib/api_wrapper/cache/cache_policy.rb', line 52 def use_cache?(endpoint) !@no_cache_endpoints.include?(endpoint) end |