Skip to content

Instantly share code, notes, and snippets.

@sillypog
Created July 8, 2016 01:06
Show Gist options
  • Save sillypog/1545b327e3d35853d66c29d1051c8e2a to your computer and use it in GitHub Desktop.
Save sillypog/1545b327e3d35853d66c29d1051c8e2a to your computer and use it in GitHub Desktop.
class Cache
def self.fetch(table_name, fields, &block)
result = redis.get(table_name + ':' + fields.values.join('|'))
unless result
# block allows us to try to look for the value elsewhere, if requested
# NOTE: we are not currently passing a block, so this syntax is sufficiently fast
# However, when a block is present it is faster to use block_given? and yield
if block
result = block.call
end
unless result.nil?
redis.set(table_name + ':' + fields.values.join('|'), result)
end
end
!result || result.empty? ? nil : result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment