诗歌rails之model_cache_version
1 require 'user_sound/has/model_cache_version'
2 module UserSound
3 module Has #:nodoc:
4 module Version #:nodoc:
5 def self.included(base)
6 base.extend(ClassMethods)
7 end
8
9 module ClassMethods
10 def has_cache_groups(*cache_groups)
11
12 end
13 def has_cache_version_for(*cache_names)
14 cache_names = cache_names.map {|name| name.to_s}
15
16 write_inheritable_attribute :cache_names, cache_names
17 class_inheritable_reader :cache_names
18
19 self.send(:include, UserSound::Has::Version::InstanceMethods)
20
21 cache_names.each do |cache_name|
22 class_eval <<-EOV
23 def #{cache_name}_version
24 read_or_create_version('#{cache_name}')
25 end
26
27 def #{cache_name}_version_number
28 #{cache_name}_version.version
29 end
30
31 def inc_#{cache_name}_version
32 inc_version('#{cache_name}')
33 end
34 EOV
35 end
36
37 after_create :create_versions
38 before_destroy :remove_versions
39 end
40
41 end
42
43 module InstanceMethods
44 def inc_versions(*cache_names)
45 cache_names.each do |cache_name|
46 inc_version(cache_name)
47 end
48 end
49 private
50 def create_versions
51 self.class.cache_names.each do |cache_name|
52 create_version(cache_name)
53 end
54 end
55
56 def create_new_version(cache_name)
57 version = UserSound::Has::Version::ModelCacheVersion.create(
58 :model_class=>self.class.name,
59 :model_id => self.id,
60 :cache_name =>cache_name,
61 :version => 0 )
62 version
63 end
64
65 def create_version(cache_name)
66 version=create_new_version(cache_name)
67 Rails.cache.write("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}", version)
68 version
69 end
70
71 def remove_versions
72 UserSound::Has::Version::ModelCacheVersion.delete_all(['model_id = ? and model_class= ?',self.id, self.class.name])
73 self.class.cache_names.each do |cache_name|
74 Rails.cache.delete("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}")
75 end
76 end
77
78 def read_or_create_version(cache_name)
79 Rails.cache.fetch("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}") do
80 version=UserSound::Has::Version::ModelCacheVersion.find(:first,
81 :conditions=>['model_id = ? and model_class= ? and cache_name=?',
82 self.id, self.class.name, cache_name])
83 version=create_new_version(cache_name) unless version
84 version
85 end
86 end
87 def inc_version(cache_name)
88 read_or_create_version(cache_name)
89 UserSound::Has::Version::ModelCacheVersion.update_all("version=version+1", "model_id = #{self.id} and model_class= '#{self.class.name}' and cache_name='#{cache_name}'")
90 Rails.cache.delete("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}")
91 end
92 end
93 end
94 end
95 end
2 module UserSound
3 module Has #:nodoc:
4 module Version #:nodoc:
5 def self.included(base)
6 base.extend(ClassMethods)
7 end
8
9 module ClassMethods
10 def has_cache_groups(*cache_groups)
11
12 end
13 def has_cache_version_for(*cache_names)
14 cache_names = cache_names.map {|name| name.to_s}
15
16 write_inheritable_attribute :cache_names, cache_names
17 class_inheritable_reader :cache_names
18
19 self.send(:include, UserSound::Has::Version::InstanceMethods)
20
21 cache_names.each do |cache_name|
22 class_eval <<-EOV
23 def #{cache_name}_version
24 read_or_create_version('#{cache_name}')
25 end
26
27 def #{cache_name}_version_number
28 #{cache_name}_version.version
29 end
30
31 def inc_#{cache_name}_version
32 inc_version('#{cache_name}')
33 end
34 EOV
35 end
36
37 after_create :create_versions
38 before_destroy :remove_versions
39 end
40
41 end
42
43 module InstanceMethods
44 def inc_versions(*cache_names)
45 cache_names.each do |cache_name|
46 inc_version(cache_name)
47 end
48 end
49 private
50 def create_versions
51 self.class.cache_names.each do |cache_name|
52 create_version(cache_name)
53 end
54 end
55
56 def create_new_version(cache_name)
57 version = UserSound::Has::Version::ModelCacheVersion.create(
58 :model_class=>self.class.name,
59 :model_id => self.id,
60 :cache_name =>cache_name,
61 :version => 0 )
62 version
63 end
64
65 def create_version(cache_name)
66 version=create_new_version(cache_name)
67 Rails.cache.write("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}", version)
68 version
69 end
70
71 def remove_versions
72 UserSound::Has::Version::ModelCacheVersion.delete_all(['model_id = ? and model_class= ?',self.id, self.class.name])
73 self.class.cache_names.each do |cache_name|
74 Rails.cache.delete("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}")
75 end
76 end
77
78 def read_or_create_version(cache_name)
79 Rails.cache.fetch("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}") do
80 version=UserSound::Has::Version::ModelCacheVersion.find(:first,
81 :conditions=>['model_id = ? and model_class= ? and cache_name=?',
82 self.id, self.class.name, cache_name])
83 version=create_new_version(cache_name) unless version
84 version
85 end
86 end
87 def inc_version(cache_name)
88 read_or_create_version(cache_name)
89 UserSound::Has::Version::ModelCacheVersion.update_all("version=version+1", "model_id = #{self.id} and model_class= '#{self.class.name}' and cache_name='#{cache_name}'")
90 Rails.cache.delete("cache_version_for_#{cache_name}/#{self.class.name}/#{self.id}")
91 end
92 end
93 end
94 end
95 end
关于write_inheritable_attribute相关调试
1 >> Company.first.cache_names
2 => ["blacklists", "customer_applyings", "customers", "employees"]
3 >> class Csub < Company
4 >> end
5 => nil
6 >> Csub.cache_names
7 => ["blacklists", "customer_applyings", "customers", "employees"]
8 >> Csub.new.cache_names
9 => ["blacklists", "customer_applyings", "customers", "employees"]
10 >> Csub.write_inheritable_attribute(:newkey,"newvalue"
11 >> )
12 => "newvalue"
13 >> Csub.read_inheritable_attribute(:newkey)
14 => "newvalue"
15 >> Csub.read_inheritable_attribute(:cache_names)
16 => ["blacklists", "customer_applyings", "customers", "employees"]
17
2 => ["blacklists", "customer_applyings", "customers", "employees"]
3 >> class Csub < Company
4 >> end
5 => nil
6 >> Csub.cache_names
7 => ["blacklists", "customer_applyings", "customers", "employees"]
8 >> Csub.new.cache_names
9 => ["blacklists", "customer_applyings", "customers", "employees"]
10 >> Csub.write_inheritable_attribute(:newkey,"newvalue"
11 >> )
12 => "newvalue"
13 >> Csub.read_inheritable_attribute(:newkey)
14 => "newvalue"
15 >> Csub.read_inheritable_attribute(:cache_names)
16 => ["blacklists", "customer_applyings", "customers", "employees"]
17
莫愁前路无知己,天下无人不识君。