Class: CacheStache::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/cache_stache/configuration.rb

Defined Under Namespace

Classes: KeyspaceBuilder

Constant Summary collapse

DEFAULT_REDIS_OPTIONS =
{reconnect_attempts: 0}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cache_stache/configuration.rb', line 14

def initialize
  self.bucket_seconds = 5.minutes
  self.retention_seconds = 7.days
  @sample_rate = 1.0
  @enabled = rails_env != "test"
  @use_rack_after_reply = false
  @redis = ENV.fetch("CACHE_STACHE_REDIS_URL") { ENV.fetch("REDIS_URL", "redis://localhost:6379/0") }
  @redis_pool_size = 5
  @max_buckets = 288
  @keyspaces = []
  @keyspace_cache = {}
end

Instance Attribute Details

#bucket_secondsObject

Returns the value of attribute bucket_seconds.



12
13
14
# File 'lib/cache_stache/configuration.rb', line 12

def bucket_seconds
  @bucket_seconds
end

#enabledObject

Returns the value of attribute enabled.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def enabled
  @enabled
end

#keyspacesObject (readonly)

Returns the value of attribute keyspaces.



12
13
14
# File 'lib/cache_stache/configuration.rb', line 12

def keyspaces
  @keyspaces
end

#max_bucketsObject

Returns the value of attribute max_buckets.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def max_buckets
  @max_buckets
end

#redisObject

Returns the value of attribute redis.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def redis
  @redis
end

#redis_pool_sizeObject

Returns the value of attribute redis_pool_size.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def redis_pool_size
  @redis_pool_size
end

#retention_secondsObject

Returns the value of attribute retention_seconds.



12
13
14
# File 'lib/cache_stache/configuration.rb', line 12

def retention_seconds
  @retention_seconds
end

#sample_rateObject

Returns the value of attribute sample_rate.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def sample_rate
  @sample_rate
end

#use_rack_after_replyObject

Returns the value of attribute use_rack_after_reply.



10
11
12
# File 'lib/cache_stache/configuration.rb', line 10

def use_rack_after_reply
  @use_rack_after_reply
end

Instance Method Details

#build_redisObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cache_stache/configuration.rb', line 44

def build_redis
  case redis
  when Proc
    redis.call
  when String
    ::Redis.new(DEFAULT_REDIS_OPTIONS.merge(url: redis))
  else
    redis
  end
end

#keyspace(name, &block) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cache_stache/configuration.rb', line 55

def keyspace(name, &block)
  ks = Keyspace.new(name)
  builder = KeyspaceBuilder.new(ks)
  builder.instance_eval(&block) if block_given?
  ks.validate!

  raise Error, "Keyspace #{name} already defined" if @keyspaces.any? { |k| k.name == name }

  @keyspaces << ks
  ks
end

#matching_keyspaces(key) ⇒ Object



67
68
69
70
71
# File 'lib/cache_stache/configuration.rb', line 67

def matching_keyspaces(key)
  # Simple memoization per key to avoid repeated block execution
  cache_key = key_digest(key)
  @keyspace_cache[cache_key] ||= @keyspaces.select { |ks| ks.match?(key) }
end

#rails_envObject



92
93
94
# File 'lib/cache_stache/configuration.rb', line 92

def rails_env
  @rails_env ||= ENV.fetch("RAILS_ENV", "development")
end

#validate!Object

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cache_stache/configuration.rb', line 73

def validate!
  raise Error, "bucket_seconds must be positive" unless bucket_seconds.to_i.positive?
  raise Error, "retention_seconds must be positive" unless retention_seconds.to_i.positive?
  raise Error, "redis must be configured" if redis.nil?
  raise Error, "redis must be a Proc, String (URL), or Redis-compatible object" unless valid_redis_option?
  raise Error, "redis_pool_size must be positive" unless redis_pool_size.to_i.positive?
  raise Error, "sample_rate must be between 0 and 1" unless sample_rate&.between?(0, 1)
  raise Error, "max_buckets must be positive" unless max_buckets.to_i.positive?

  if retention_seconds % bucket_seconds != 0
    Rails.logger.warn(
      "CacheStache: retention_seconds (#{retention_seconds}) does not divide evenly " \
      "by bucket_seconds (#{bucket_seconds}). This may result in partial bucket retention."
    )
  end

  @keyspaces.each(&:validate!)
end