second_level_cache 1.5.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -27,4 +27,10 @@
27
27
  * [fix errors when belongs_to association return nil]
28
28
 
29
29
  1.5.0
30
+ -----
30
31
  * [add cache version to quick clear cache for special model]
32
+
33
+ 1.5.1
34
+ -----
35
+ * [use new marshal machanism to avoid clear assocation cache manually]
36
+
@@ -0,0 +1,27 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module RecordMarshal
3
+ class << self
4
+ # dump ActiveRecord instace with only attributes.
5
+ # ["User",
6
+ # {"id"=>30,
7
+ # "email"=>"dddssddd@gmail.com",
8
+ # "created_at"=>2012-07-25 18:25:57 UTC
9
+ # }
10
+ # ]
11
+
12
+ def dump(record)
13
+ [
14
+ record.class.name,
15
+ record.instance_variable_get(:@attributes)
16
+ ]
17
+ end
18
+
19
+ # load a cached record
20
+ def load(serialized)
21
+ return unless serialized
22
+ record = serialized[0].constantize.allocate
23
+ record.init_with('attributes' => serialized[1])
24
+ record
25
+ end
26
+ end
27
+ end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module SecondLevelCache
3
- VERSION = "1.5.0"
3
+ VERSION = "1.5.1"
4
4
  end
@@ -1,7 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'active_support/all'
3
3
  require 'second_level_cache/config'
4
- require 'second_level_cache/marshal'
4
+ require 'second_level_cache/record_marshal'
5
5
 
6
6
  module SecondLevelCache
7
7
  def self.configure
@@ -50,7 +50,7 @@ module SecondLevelCache
50
50
  end
51
51
 
52
52
  def read_second_level_cache(id)
53
- SecondLevelCache.cache_store.read(second_level_cache_key(id)) if self.second_level_cache_enabled?
53
+ RecordMarshal.load(SecondLevelCache.cache_store.read(second_level_cache_key(id))) if self.second_level_cache_enabled?
54
54
  end
55
55
 
56
56
  def expire_second_level_cache(id)
@@ -68,8 +68,7 @@ module SecondLevelCache
68
68
 
69
69
  def write_second_level_cache
70
70
  if self.class.second_level_cache_enabled?
71
- self.clear_association_cache
72
- SecondLevelCache.cache_store.write(second_level_cache_key, self, :expires_in => self.class.second_level_cache_options[:expires_in])
71
+ SecondLevelCache.cache_store.write(second_level_cache_key, RecordMarshal.dump(self), :expires_in => self.class.second_level_cache_options[:expires_in])
73
72
  end
74
73
  end
75
74
  end
@@ -10,7 +10,6 @@ class ActiveRecord::PolymorphicAssociationTest < Test::Unit::TestCase
10
10
  image = @user.images.create
11
11
 
12
12
  @user.write_second_level_cache
13
- image.clear_association_cache
14
13
  no_connection do
15
14
  assert_equal @user, image.imagable
16
15
  end
@@ -18,7 +17,6 @@ class ActiveRecord::PolymorphicAssociationTest < Test::Unit::TestCase
18
17
 
19
18
  def test_should_write_polymorphic_association_cache
20
19
  image = @user.images.create
21
- @user.expire_second_level_cache
22
20
  assert_nil User.read_second_level_cache(@user.id)
23
21
  assert_equal @user, image.imagable
24
22
  end
@@ -0,0 +1,33 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'active_record/test_helper'
3
+
4
+ class RecordMarshalTest < Test::Unit::TestCase
5
+ def setup
6
+ @user = User.create :name => 'csdn', :email => 'test@csdn.com'
7
+ end
8
+
9
+ def test_should_dump_active_record_object
10
+ dumped = RecordMarshal.dump(@user)
11
+ assert dumped.is_a?(Array)
12
+ assert_equal "User", dumped[0]
13
+ assert_equal @user.attributes, dumped[1]
14
+ end
15
+
16
+
17
+ def test_should_load_active_record_object
18
+ @user.write_second_level_cache
19
+ assert_equal @user, User.read_second_level_cache(@user.id)
20
+ end
21
+
22
+
23
+ def test_should_load_nil
24
+ @user.expire_second_level_cache
25
+ assert_nil User.read_second_level_cache(@user.id)
26
+ end
27
+
28
+ def test_should_load_active_record_object_without_association_cache
29
+ @user.books
30
+ @user.write_second_level_cache
31
+ assert_empty User.read_second_level_cache(@user.id).association_cache
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: second_level_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-24 00:00:00.000000000 Z
12
+ date: 2012-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -99,7 +99,7 @@ files:
99
99
  - lib/second_level_cache/active_record/persistence.rb
100
100
  - lib/second_level_cache/arel/wheres.rb
101
101
  - lib/second_level_cache/config.rb
102
- - lib/second_level_cache/marshal.rb
102
+ - lib/second_level_cache/record_marshal.rb
103
103
  - lib/second_level_cache/version.rb
104
104
  - second_level_cache.gemspec
105
105
  - test/active_record/base_test.rb
@@ -114,6 +114,7 @@ files:
114
114
  - test/active_record/polymorphic_association_test.rb
115
115
  - test/active_record/second_level_cache_test.rb
116
116
  - test/active_record/test_helper.rb
117
+ - test/record_marshal_test.rb
117
118
  - test/test_helper.rb
118
119
  homepage: https://github.com/csdn-dev/second_level_cache
119
120
  licenses: []
@@ -127,18 +128,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
128
  - - ! '>='
128
129
  - !ruby/object:Gem::Version
129
130
  version: '0'
130
- segments:
131
- - 0
132
- hash: -453317869
133
131
  required_rubygems_version: !ruby/object:Gem::Requirement
134
132
  none: false
135
133
  requirements:
136
134
  - - ! '>='
137
135
  - !ruby/object:Gem::Version
138
136
  version: '0'
139
- segments:
140
- - 0
141
- hash: -453317869
142
137
  requirements: []
143
138
  rubyforge_project:
144
139
  rubygems_version: 1.8.24
@@ -1,16 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- module Marshal
3
- class << self
4
- def load_with_constantize(value)
5
- begin
6
- Marshal.load_without_constantize value
7
- rescue ArgumentError => e
8
- _, class_name = *(/undefined class\/module (\w+)/.match(e.message))
9
- raise if !class_name
10
- class_name.constantize
11
- Marshal.load value
12
- end
13
- end
14
- alias_method_chain :load, :constantize
15
- end
16
- end