Fixed lookup cache management.

This commit is contained in:
Martin Edenhofer 2013-03-11 22:15:21 +01:00
parent 775296bfc9
commit 852339be1e
2 changed files with 83 additions and 0 deletions

View file

@ -3,6 +3,9 @@ require 'cache'
class ApplicationModel < ActiveRecord::Base
self.abstract_class = true
before_create :cache_delete
before_update :cache_delete_before
before_destroy :cache_delete_before
after_create :cache_delete
after_update :cache_delete
after_destroy :cache_delete
@ -40,12 +43,33 @@ class ApplicationModel < ActiveRecord::Base
# puts 'g ' + group.class.to_s
if o.respond_to?('cache_delete') then o.cache_delete end
end
def cache_delete_before
old_object = self.class.where( :id => self.id ).first
if old_object
old_object.cache_delete
end
self.cache_delete
end
def cache_delete
key = self.class.to_s + '::' + self.id.to_s
Cache.delete( key.to_s )
key = self.class.to_s + ':f:' + self.id.to_s
Cache.delete( key.to_s )
if self[:name]
key = self.class.to_s + '::' + self.name.to_s
Cache.delete( key.to_s )
key = self.class.to_s + ':f:' + self.name.to_s
Cache.delete( key.to_s )
end
if self[:login]
key = self.class.to_s + '::' + self.login.to_s
Cache.delete( key.to_s )
key = self.class.to_s + ':f:' + self.login.to_s
Cache.delete( key.to_s )
end
end
def self.cache_set(data_id, data, full = false)
if !full
key = self.to_s + '::' + data_id.to_s

View file

@ -0,0 +1,59 @@
# encoding: utf-8
require 'test_helper'
class ObjectCacheTest < ActiveSupport::TestCase
test 'object cache' do
name = 'object cache test ' + rand(9999999).to_s
group = Group.create(
:name => name,
:updated_by_id => 1,
:created_by_id => 1,
)
group_where = Group.where( :name => name ).first
assert_equal( name, group_where[:name], 'verify by where' )
group_lookup_name = Group.lookup( :name => name )
assert_equal( name, group_lookup_name[:name], 'verify by lookup.name' )
group_lookup_id = Group.lookup( :id => group.id )
assert_equal( name, group_lookup_id[:name], 'verify by lookup.id' )
name_new = name + ' next'
group.name = name_new
group.save
group_where = Group.where( :name => name ).first
assert_equal( nil, group_where, 'verify by where name_old' )
group_where = Group.where( :name => name_new ).first
assert_equal( name_new, group_where[:name], 'verify by where name_new' )
group_lookup_name = Group.lookup( :name => name )
assert_equal( nil, group_lookup_name, 'verify by lookup.name name_old' )
group_lookup_name = Group.lookup( :name => name_new )
assert_equal( name_new, group_lookup_name[:name], 'verify by lookup.name name_new' )
group_lookup_id = Group.lookup( :id => group.id )
assert_equal( name_new, group_lookup_id[:name], 'verify by lookup.id' )
group.destroy
group_where = Group.where( :name => name ).first
assert_equal( nil, group_where, 'verify by where name_old' )
group_where = Group.where( :name => name_new ).first
assert_equal( nil, group_where, 'verify by where name_new' )
group_lookup_name = Group.lookup( :name => name )
assert_equal( nil, group_lookup_name, 'verify by lookup.name name_old' )
group_lookup_name = Group.lookup( :name => name_new )
assert_equal( nil, group_lookup_name, 'verify by lookup.name name_new' )
group_lookup_id = Group.lookup( :id => group.id )
assert_equal( nil, group_lookup_id, 'verify by lookup.id' )
end
end