Module: Greencache

Defined in:
lib/greencache.rb,
lib/greencache/version.rb,
lib/greencache/configuration.rb

Defined Under Namespace

Classes: CacheMiss, Configuration

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



91
92
93
# File 'lib/greencache.rb', line 91

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.cache(redis_key, config = {}, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/greencache.rb', line 21

def cache(redis_key, config = {}, &block)
  config = merge_config(config)
  return block.call if config[:skip_cache] || !redis_up?
  read_from_cache!(redis_key, config)
rescue CacheMiss
  value = block.call
  write_into_cache(redis_key, value, config)
  value
end

.configure {|configuration| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/greencache.rb', line 17

def configure
  yield(configuration)
end

.decrypt(value, config) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/greencache.rb', line 83

def decrypt(value, config)
  return nil if value.nil?
  return MultiJson.load(value) unless config[:encrypt]
  verifier = fernet.verifier(config[:secret], value)
  return MultiJson.load(verifier.message) if verifier.valid?
  return nil
end

.encrypt(value, config) ⇒ Object



78
79
80
81
# File 'lib/greencache.rb', line 78

def encrypt(value, config)
  return prep_value(value) unless config[:encrypt]
  fernet.generate(config[:secret], prep_value(value))
end

.expire(redis_key) ⇒ Object



31
32
33
# File 'lib/greencache.rb', line 31

def expire(redis_key)
  redis.del(redis_key) if redis_up?
end

.fernetObject



129
130
131
# File 'lib/greencache.rb', line 129

def fernet
  ::Fernet
end

.get_value(key, config) ⇒ Object



64
65
66
67
# File 'lib/greencache.rb', line 64

def get_value(key, config)
  get_value!(key, config)
rescue CacheMiss
end

.log(str, key, config) ⇒ Object



121
122
123
# File 'lib/greencache.rb', line 121

def log(str, key, config)
  config[:logger].log(log_prefix(str, config) => 1, :key => key) unless config[:silent]
end

.log_prefix(str, config) ⇒ Object



125
126
127
# File 'lib/greencache.rb', line 125

def log_prefix(str, config)
  [config[:log_prefix], str].join(".")
end

.memory_cacheObject



133
134
135
# File 'lib/greencache.rb', line 133

def memory_cache
  @@memory_cache ||= MiniCache::Store.new
end

.merge_config(config) ⇒ Object



74
75
76
# File 'lib/greencache.rb', line 74

def merge_config(config)
  configuration.to_hash.merge(config)
end

.prep_value(value) ⇒ Object



117
118
119
# File 'lib/greencache.rb', line 117

def prep_value(value)
  MultiJson.encode(value)
end

.read_from_cacheObject



44
45
46
47
# File 'lib/greencache.rb', line 44

def read_from_cache
  read_from_cache!(redis_key)
rescue CacheMiss
end

.redisObject



95
96
97
# File 'lib/greencache.rb', line 95

def redis
  configuration.redis
end

.redis_up?Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
# File 'lib/greencache.rb', line 99

def redis_up?
  begin
    redis.ping
  rescue Redis::CannotConnectError, Timeout::Error
    puts "Redis is DOWN! :shitsonfire:"
    return false
  end
  return true
end

.set_value(key, value, config) ⇒ Object



69
70
71
72
# File 'lib/greencache.rb', line 69

def set_value(key, value, config)
  redis.setex key, config[:cache_time], encrypt(value, config)
  memory_cache.set(key, value, expires_in: 1)
end

.test?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/greencache.rb', line 113

def test?
  ENV["RACK_ENV"] == 'test'
end

.with_redis(&block) ⇒ Object



109
110
111
# File 'lib/greencache.rb', line 109

def with_redis(&block)
  block.call if redis_up?
end

.write_into_cache(redis_key, value, config) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/greencache.rb', line 49

def write_into_cache(redis_key, value, config)
  with_redis do
    log("cache.write", redis_key, config)
    set_value(redis_key, value, config)
  end
  value
end