123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
- module ApplicationModel::HasCache
- extend ActiveSupport::Concern
- included do
- before_create :cache_delete
- after_create :cache_delete
- after_update :cache_delete
- after_touch :cache_delete
- after_destroy :cache_delete
- end
- def cache_update(o)
- cache_delete if respond_to?('cache_delete')
- o.cache_delete if o.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
- if saved_changes?
- if saved_changes.key?('name')
- name = saved_changes['name'][0]
- key = "#{self.class}::#{name}"
- Cache.delete(key)
- end
- 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
|