Skip to content

Instantly share code, notes, and snippets.

@sillypog
Last active July 8, 2016 00:54
Show Gist options
  • Save sillypog/816f23d34ec68411484ec4fa2d491e83 to your computer and use it in GitHub Desktop.
Save sillypog/816f23d34ec68411484ec4fa2d491e83 to your computer and use it in GitHub Desktop.
class AbstractDimensionTable
def self.call_remote_cache(fields)
Cache.fetch(table_name, fields) do
call_redshift fields
end
end
end
class Cache
def self.fetch(table_name, fields, &block)
# Try going to redis first
# If there is no value in there, call the block that was passed in
# If a value comes back from the block, add it to the cache and return value
result = redis.get(table_name + ':' + fields.values.join('|'))
unless result
result = block.call
unless result.nil?
redis.set(table_name + ':' + fields.values.join('|'), result)
end
end
# An 'empty' result from Redshift will be nil,
# But an 'empty' result from Redis is an empty string
# Return nil in either case
!result || result.empty? ? nil : result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment