Class: DiscordRDA::ConfigurableCache
- Inherits:
-
Object
- Object
- DiscordRDA::ConfigurableCache
- Defined in:
- lib/discord_rda/cache/configurable_cache.rb
Overview
Configurable cache manager. By default, caches NOTHING - follows Discordeno philosophy. Users can opt-in to caching only what they need.
Constant Summary collapse
- STRATEGY_NONE =
Cache nothing strategy
:none- STRATEGY_FULL =
Cache everything strategy
:full- STRATEGY_CUSTOM =
Custom strategy - user specifies what to cache
:custom
Instance Attribute Summary collapse
-
#cached_properties ⇒ Array<Symbol>
readonly
Properties to cache per entity.
-
#enabled_caches ⇒ Hash
readonly
Enabled cache types.
-
#logger ⇒ Logger
readonly
Logger instance.
-
#store ⇒ CacheStore
readonly
Cache store backend.
-
#strategy ⇒ Symbol
readonly
Current cache strategy.
Instance Method Summary collapse
-
#cache(type, id, entity, ttl: 300) ⇒ void
Cache an entity (only if enabled for this type).
-
#clear ⇒ void
Clear all cached data.
-
#get(type, id) ⇒ Entity?
Get cached entity.
-
#initialize(strategy: STRATEGY_NONE, store: nil, logger: nil, enabled_caches: [], cached_properties: {}) ⇒ ConfigurableCache
constructor
Initialize configurable cache.
-
#invalidate(type, id) ⇒ void
Invalidate an entity.
-
#should_cache?(type) ⇒ Boolean
Check if entity should be cached.
-
#stats ⇒ Hash
Get cache statistics.
-
#with(**overrides) ⇒ ConfigurableCache
Create a new cache with different settings (immutable).
Constructor Details
#initialize(strategy: STRATEGY_NONE, store: nil, logger: nil, enabled_caches: [], cached_properties: {}) ⇒ ConfigurableCache
Initialize configurable cache
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 39 def initialize( strategy: STRATEGY_NONE, store: nil, logger: nil, enabled_caches: [], cached_properties: {} ) @strategy = strategy @store = store || MemoryStore.new @logger = logger @enabled_caches = enabled_caches @cached_properties = cached_properties @logger&.info('Cache initialized', strategy: strategy, enabled: enabled_caches) end |
Instance Attribute Details
#cached_properties ⇒ Array<Symbol> (readonly)
Returns Properties to cache per entity.
31 32 33 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 31 def cached_properties @cached_properties end |
#enabled_caches ⇒ Hash (readonly)
Returns Enabled cache types.
28 29 30 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 28 def enabled_caches @enabled_caches end |
#logger ⇒ Logger (readonly)
Returns Logger instance.
25 26 27 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 25 def logger @logger end |
#store ⇒ CacheStore (readonly)
Returns Cache store backend.
22 23 24 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 22 def store @store end |
#strategy ⇒ Symbol (readonly)
Returns Current cache strategy.
19 20 21 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 19 def strategy @strategy end |
Instance Method Details
#cache(type, id, entity, ttl: 300) ⇒ void
This method returns an undefined value.
Cache an entity (only if enabled for this type)
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 61 def cache(type, id, entity, ttl: 300) return unless should_cache?(type) # If custom properties specified, only cache those if @cached_properties[type] entity = filter_properties(entity, @cached_properties[type]) end key = "#{type}:#{id}" @store.set(key, entity, ttl: ttl) @logger&.debug('Cached entity', type: type, id: id, strategy: @strategy) end |
#clear ⇒ void
This method returns an undefined value.
Clear all cached data
113 114 115 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 113 def clear @store.clear end |
#get(type, id) ⇒ Entity?
Get cached entity
79 80 81 82 83 84 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 79 def get(type, id) return nil unless should_cache?(type) key = "#{type}:#{id}" @store.get(key) end |
#invalidate(type, id) ⇒ void
This method returns an undefined value.
Invalidate an entity
106 107 108 109 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 106 def invalidate(type, id) key = "#{type}:#{id}" @store.delete(key) end |
#should_cache?(type) ⇒ Boolean
Check if entity should be cached
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 89 def should_cache?(type) case @strategy when STRATEGY_NONE false when STRATEGY_FULL true when STRATEGY_CUSTOM @enabled_caches.include?(type) else false end end |
#stats ⇒ Hash
Get cache statistics
119 120 121 122 123 124 125 126 127 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 119 def stats base_stats = @store.respond_to?(:stats) ? @store.stats : {} { strategy: @strategy, enabled_caches: @enabled_caches, **base_stats } end |
#with(**overrides) ⇒ ConfigurableCache
Create a new cache with different settings (immutable)
132 133 134 135 136 137 138 139 140 |
# File 'lib/discord_rda/cache/configurable_cache.rb', line 132 def with(**overrides) self.class.new( strategy: overrides.fetch(:strategy, @strategy), store: overrides.fetch(:store, @store), logger: overrides.fetch(:logger, @logger), enabled_caches: overrides.fetch(:enabled_caches, @enabled_caches), cached_properties: overrides.fetch(:cached_properties, @cached_properties) ) end |