Skip to content

Instantly share code, notes, and snippets.

@tsnow
Last active August 29, 2015 13:56
Show Gist options
  • Save tsnow/9278946 to your computer and use it in GitHub Desktop.
Save tsnow/9278946 to your computer and use it in GitHub Desktop.
class CachedMethod
def initialize(name, cache_args={:expires_in => 1.minute},&method)
@name = name
@action = method
@cache_args = cache_args
end
def self.get(name,cache_args,&method)
cache = new(name,cache_args,&method)
cache.get
end
def get
Rails.cache.read(@name, @cache_args) do
@action.call
end
end
def delete
Rails.cache.delete(@name)
end
end
class KeyValueCache < CachedMethod
def get
Hash[*(super().map{|x| [x.key, x.value]}.flatten)]
end
end
## OLD
#returns a hash of providers who should send their events to fleetmagic with the keys being the provider_ids and the values being the provider keynames
def self.replicates_data_to_fleet_magic_cached
providers_hash = Rails.cache.fetch("/providers/replicates_data_to_fleet_magic", :expires_in => 1.minute) do
providers = Hash[*ProviderService.replicates_data_to_fleet_magic.all(:select => "id, key_name").map{|x| [x.id, x.key_name]}.flatten]
end
end
## NEW
def self.replicates_data_to_fleet_magic_cached
KeyValueCache.get("/providers/replicates_data_to_fleet_magic") do
ProviderService.replicates_data_to_fleet_magic.all(:select => "id as key, key_name as value")
end
end
def self.uses_vehicle_wizard_cached
KeyValueCache.get("/providers/uses_vehicle_wizard", :expires_in => 10.minute) do
ProviderService.uses_vehicle_wizard.all(:select => "id as key, key_name as value")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment