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.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



81
82
83
# File 'lib/greencache.rb', line 81

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

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



18
19
20
21
22
23
24
25
26
# File 'lib/greencache.rb', line 18

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:



14
15
16
# File 'lib/greencache.rb', line 14

def configure
  yield(configuration)
end

.decrypt(value, config) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/greencache.rb', line 73

def decrypt(value, config)
  return nil if value.nil?
  return 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



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

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

.fernetObject



119
120
121
# File 'lib/greencache.rb', line 119

def fernet
  ::Fernet
end

.get_value(key, config) ⇒ Object



55
56
57
58
# File 'lib/greencache.rb', line 55

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

.log(str, key, config) ⇒ Object



111
112
113
# File 'lib/greencache.rb', line 111

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

.log_prefix(str, config) ⇒ Object



115
116
117
# File 'lib/greencache.rb', line 115

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

.merge_config(config) ⇒ Object



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

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

.prep_value(value) ⇒ Object



107
108
109
# File 'lib/greencache.rb', line 107

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

.read_from_cacheObject



37
38
39
40
# File 'lib/greencache.rb', line 37

def read_from_cache
  read_from_cache!(redis_key)
rescue CacheMiss
end

.redisObject



85
86
87
# File 'lib/greencache.rb', line 85

def redis
  configuration.redis
end

.redis_up?Boolean

Returns:



89
90
91
92
93
94
95
96
97
# File 'lib/greencache.rb', line 89

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



60
61
62
# File 'lib/greencache.rb', line 60

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

.test?Boolean

Returns:



103
104
105
# File 'lib/greencache.rb', line 103

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

.with_redis(&block) ⇒ Object



99
100
101
# File 'lib/greencache.rb', line 99

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

.write_into_cache(redis_key, value, config) ⇒ Object



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

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