Skip to content

Commit

Permalink
Merge pull request #3 from pfeiffer/refactor_cache
Browse files Browse the repository at this point in the history
Refactor caching code, allow setting it per client
  • Loading branch information
onefriendaday authored Oct 16, 2018
2 parents d268480 + 8038255 commit 8fe8d8c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
2 changes: 1 addition & 1 deletion lib/storyblok.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'storyblok/version'
require 'storyblok/cache'
require 'storyblok/cache/redis'
require 'storyblok/client'
26 changes: 0 additions & 26 deletions lib/storyblok/cache.rb

This file was deleted.

51 changes: 51 additions & 0 deletions lib/storyblok/cache/redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Storyblok
module Cache
class Redis
DEFAULT_CONFIGURATION = {
ttl: 60 * 60 * 24
}

def initialize(*args)
options = args.extract_options!

@redis = options.delete(:redis) || begin
if defined?(::Redis)
::Redis.current
else
raise "Redis.current could not be found. Supply :redis option or make sure Redis.current is available."
end
end

@options = DEFAULT_CONFIGURATION.merge(options)
end

def cache(key, expire = nil)
if expire == 0
return yield(self)
end

expire ||= @options[:ttl]

if (value = get(key)).nil?
value = yield(self)
set(key, value, expire)
end

value
end

def get(key)
@redis.get(key)
end

def set(key, value, expire = false)
if expire
@redis.setex(key, expire, value)
else
@redis.set(key, value)
end
end

end
end
end
18 changes: 11 additions & 7 deletions lib/storyblok/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Client
logger: false,
log_level: Logger::INFO,
version: 'draft',
cache_version: Time.now.to_i
cache_version: Time.now.to_i,
cache: nil,
}

attr_reader :configuration, :logger
Expand Down Expand Up @@ -92,14 +93,13 @@ def get(request)
query = request_query(request.query)
query_string = build_nested_query(query)

if Cache.client.nil?
if cache.nil?
result = run_request(endpoint, query_string)
else
version = Cache.client.get('storyblok:' + configuration[:token] + ':version') || '0'
version = cache.get('storyblok:' + configuration[:token] + ':version') || '0'
cache_key = 'storyblok:' + configuration[:token] + ':v:' + version + ':' + request.url + ':' + Base64.encode64(query_string)
cache_time = 60 * 60 * 2

result = Cache.cache(cache_key, cache_time) do
result = cache.cache(cache_key) do
run_request(endpoint, query_string)
end
end
Expand All @@ -108,8 +108,8 @@ def get(request)
end

def flush
if !Cache.client.nil?
Cache.client.set('storyblok:' + configuration[:token] + ':version', Time.now.to_i.to_s)
unless cache.nil?
cache.set('storyblok:' + configuration[:token] + ':version', Time.now.to_i.to_s)
end
end

Expand Down Expand Up @@ -139,6 +139,10 @@ def default_configuration
DEFAULT_CONFIGURATION.dup
end

def cache
configuration[:cache]
end

def setup_logger
@logger = configuration[:logger]
logger.level = configuration[:log_level] if logger
Expand Down

0 comments on commit 8fe8d8c

Please sign in to comment.