Last active
June 7, 2017 21:16
-
-
Save shaiguitar/6d8a16b9c776686c33478cc12c648b8e to your computer and use it in GitHub Desktop.
fog-core caching class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyCache | |
def self.all(resource_klass, connection, meth, args = nil) | |
cachemsg = "resource #{resource_klass} connection #{connection.class} for #{Fog::Cache.namespace_prefix}: M:#{meth} A:#{args}" | |
if MyCache.renew? | |
info("Renewing cache now. Cache is non existant or older than #{EXPIRE_TIME} seconds.") | |
Fog::Cache.expire_cache!(resource_klass, connection) | |
end | |
get_from_api = lambda { | |
# metaprogramming voodoo shortcut for eg: iam.users.all(iam_opts) or compute.servers.all(filters) or elb.load_balancers.all | |
if args | |
connection.send(meth).send(:all, args) | |
else | |
connection.send(meth).all | |
end | |
} | |
if ENV['MYCACHE_NOCACHE'] | |
info("Force bypass using cache - querying aws directly.") | |
resources = get_from_api.call | |
else | |
######### BULK OF CACHE STEPS / EXAMPLE USAGE ############## | |
# 1) first try to get the resources from cache: | |
begin | |
resources = Fog::Cache.load(resource_klass, connection) | |
debug("CACHE: found cache data for #{cachemsg}") if resources | |
# 1a) return if we found them in the cache | |
info("Found cache for #{resource_klass}. Using this data. You can set MYCACHE_NOCACHE=1 or remove #{Fog::Cache::SANDBOX} to bypass this behavior.\n") | |
return resources | |
rescue Fog::Cache::CacheNotFound => e | |
debug("CACHE: could not find any cache data for #{cachemsg}...trying connection.") | |
# 2) not found in cache. get resources from the connection. | |
resources = get_from_api.call | |
# 3) dump resources for next time | |
resources.each { |r| r.cache.dump } | |
debug("CACHE: dumped #{resources.size} entries to cache for #{cachemsg}.") | |
# 4) dump metatata for cache expirey | |
Fog::Cache.write_metadata({:last_refreshed => Time.now}) | |
debug("CACHE: set metadata for #{Fog::Cache.namespace_prefix} to current time.") | |
# 5) return resources. | |
return resources | |
end | |
end | |
end | |
# set for different services/apps etc. | |
def self.set_session_identifier(name) | |
Fog::Cache.namespace_prefix = name | |
end | |
EXPIRE_TIME = (60 * 60 * 24) # day old cache. | |
# renew policy cache once a day, for example. | |
def self.renew? | |
last_refreshed = Fog::Cache.metadata[:last_refreshed] | |
if last_refreshed.nil? | |
# no metadata, cache is old | |
return true | |
end | |
if (Time.now - last_refreshed) > EXPIRE_TIME | |
return true | |
else | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: