Class: DiscordRDA::ConfigurableCache

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(strategy: STRATEGY_NONE, store: nil, logger: nil, enabled_caches: [], cached_properties: {}) ⇒ ConfigurableCache

Initialize configurable cache

Parameters:

  • strategy (Symbol) (defaults to: STRATEGY_NONE)

    Cache strategy (:none, :full, :custom)

  • store (CacheStore) (defaults to: nil)

    Cache store backend

  • logger (Logger) (defaults to: nil)

    Logger instance

  • enabled_caches (Array<Symbol>) (defaults to: [])

    Which entity types to cache (for :custom)

  • cached_properties (Hash) (defaults to: {})

    Which properties to cache per entity



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_propertiesArray<Symbol> (readonly)

Returns Properties to cache per entity.

Returns:

  • (Array<Symbol>)

    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_cachesHash (readonly)

Returns Enabled cache types.

Returns:

  • (Hash)

    Enabled cache types



28
29
30
# File 'lib/discord_rda/cache/configurable_cache.rb', line 28

def enabled_caches
  @enabled_caches
end

#loggerLogger (readonly)

Returns Logger instance.

Returns:

  • (Logger)

    Logger instance



25
26
27
# File 'lib/discord_rda/cache/configurable_cache.rb', line 25

def logger
  @logger
end

#storeCacheStore (readonly)

Returns Cache store backend.

Returns:

  • (CacheStore)

    Cache store backend



22
23
24
# File 'lib/discord_rda/cache/configurable_cache.rb', line 22

def store
  @store
end

#strategySymbol (readonly)

Returns Current cache strategy.

Returns:

  • (Symbol)

    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)

Parameters:

  • type (Symbol)

    Entity type

  • id (String, Snowflake)

    Entity ID

  • entity (Entity)

    Entity to cache

  • ttl (Integer) (defaults to: 300)

    Time to live in seconds



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

#clearvoid

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

Parameters:

  • type (Symbol)

    Entity type

  • id (String, Snowflake)

    Entity ID

Returns:

  • (Entity, nil)

    Cached entity or nil



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

Parameters:

  • type (Symbol)

    Entity type

  • id (String, Snowflake)

    Entity ID



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

Parameters:

  • type (Symbol)

    Entity type

Returns:

  • (Boolean)

    True if should cache



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

#statsHash

Get cache statistics

Returns:

  • (Hash)

    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)

Parameters:

  • overrides (Hash)

    Settings to override

Returns:



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