trabajo-afectivo/app/models/application_model/has_cache.rb

69 lines
1.5 KiB
Ruby
Raw Normal View History

# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HasCache
extend ActiveSupport::Concern
included do
before_create :cache_delete
after_commit :cache_delete
end
2018-04-12 14:57:37 +00:00
def cache_update(other)
cache_delete if respond_to?('cache_delete')
2018-04-12 14:57:37 +00:00
other.cache_delete if other.respond_to?('cache_delete')
true
end
def cache_delete
keys = []
# delete by id caches
keys.push "#{self.class}::#{id}"
# delete by id with attributes_with_association_ids caches
keys.push "#{self.class}::aws::#{id}"
# delete by name caches
if self[:name]
keys.push "#{self.class}::#{name}"
end
# delete by login caches
if self[:login]
keys.push "#{self.class}::#{login}"
end
keys.each do |key|
Cache.delete(key)
end
# delete old name / login caches
2017-09-23 06:25:55 +00:00
if saved_changes?
if saved_changes.key?('name')
name = saved_changes['name'][0]
key = "#{self.class}::#{name}"
Cache.delete(key)
end
2017-09-23 06:25:55 +00:00
if saved_changes.key?('login')
name = saved_changes['login'][0]
key = "#{self.class}::#{name}"
Cache.delete(key)
end
end
true
end
# methods defined here are going to extend the class, not the instance of it
class_methods do
def cache_set(data_id, data)
key = "#{self}::#{data_id}"
Cache.write(key, data)
end
def cache_get(data_id)
key = "#{self}::#{data_id}"
Cache.get(key)
end
end
end