cache-object 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3312382f303270f6529c1b4c4ff709bfc987dd79
4
- data.tar.gz: aae75915fd361af70fa6dcd89c7907dd69e08ebd
3
+ metadata.gz: 1485233396171f659819ed221040368678fbe00e
4
+ data.tar.gz: d7fdcd4e545b9a80d89118f3b2e5869f53951802
5
5
  SHA512:
6
- metadata.gz: 23a1990e9456e0e4b5b16134933e2579990757cb7863ff4d75bab0181604e734534a17afc98d25e1ceab77e0cacacbe369348857107fee4114c09bff7ad4faa0
7
- data.tar.gz: 4ba38810903f4ebd84202ca4a09da7d34682e07ce4e18babb2fc62a3365ad6293d2b76220704ce2bdd776f7fe5b924d76784ea68a229cf2d068cdaa9217f1929
6
+ metadata.gz: 7dd097dcc97365356c283c40237a74e4252689c0899ca6ce05887245ae3f803d369a15ff6d474e8a2bc92048052d263557111c9a8651486f2549ebdcc381f6a3
7
+ data.tar.gz: 8119eef50f1435f73f3c838ac92a16a18d3cf31709803ee0fc3b66b00c536848dfb41f5d9fb19a594b95a019279a3417c7a3c5d251481620b028bd43a4861243
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Cache::Object
2
2
 
3
- TODO: Write a gem description
3
+ Cache ActiveRecord objects in memcached!
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,56 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Add a Rails initializer, for instance at `config/initializers/cache-object.rb`
22
+
23
+ ```ruby
24
+ # Use the Rails cache.
25
+ Cache::Object.configure do |c|
26
+ c.cache = Rails.cache
27
+ end
28
+
29
+ # Use an arbitrary Dalli connection in your environment.
30
+ Cache::Object.configure do |c|
31
+ hosts = Array(Settings.caching.memcached.hosts).clone
32
+ hosts << {namespace: 'my.namespace',
33
+ expires_in: 1.day,
34
+ compress: true,
35
+ keepalive: true,
36
+ socket_timeout: Settings.caching.memcached.socket_timeout}
37
+
38
+ c.cache = ActiveSupport::Cache::DalliStore.new(hosts)
39
+ end
40
+ ```
41
+
42
+ Include the `Cache::Object::ActiveRecord` module into your model. When a record is saved, its attributes will be
43
+ marshalled into the cache.
44
+
45
+ ```ruby
46
+ class User < ActiveRecord::Base
47
+ include Cache::Object::ActiveRecord
48
+ end
49
+ ```
50
+
51
+ By default the record is cached by the primary id. If another key should be used, `object_cache_on` can be used:
52
+
53
+ ```ruby
54
+ class User < ActiveRecord::Base
55
+ include Cache::Object::ActiveRecord
56
+ object_cache_on :username
57
+ end
58
+ ```
59
+
60
+ If instance variables that are not ActiveRecord attributes need to be cached, `object_cache_include` can be used:
61
+
62
+ ```ruby
63
+ class User < ActiveRecord::Base
64
+ include Cache::Object::ActiveRecord
65
+ object_cache_include :shoe_size
66
+
67
+ attr_accessor :shoe_size
68
+ end
69
+ ```
70
+
22
71
 
23
72
  ## Contributing
24
73
 
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency "activerecord", ">= 3.0"
21
+ spec.add_dependency "activerecord", [">= 3.0", "< 4.2"]
22
22
  spec.add_dependency "ruby-usdt"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
@@ -11,6 +11,10 @@ module Cache
11
11
  @_object_cache_attr_mappings ||= []
12
12
  end
13
13
 
14
+ def _object_cache_include
15
+ @_object_cache_include ||= []
16
+ end
17
+
14
18
  after_destroy :expire_cache!
15
19
  after_save :write_cache!
16
20
  after_rollback :expire_cache!
@@ -18,10 +22,19 @@ module Cache
18
22
  end
19
23
 
20
24
  def _dump(level = 0)
21
- Marshal.dump(attributes)
25
+ additional_attributes = {}.tap do |h|
26
+ self.class._object_cache_include.flatten.each do |key|
27
+ h[key.to_s] = self.send(key)
28
+ end
29
+ end
30
+ Marshal.dump(attributes.merge(additional_attributes))
22
31
  end
23
32
 
24
33
  def load_from_cache(attributes)
34
+ self.class._object_cache_include.flatten.each do |key|
35
+ send("#{key}=", attributes.delete(key.to_s))
36
+ end
37
+
25
38
  @attributes = self.class.initialize_attributes(attributes)
26
39
  @relation = nil
27
40
 
@@ -74,6 +87,10 @@ module Cache
74
87
  Cache::Object::MultiGet.new(self).fetch_all(ids)
75
88
  end
76
89
 
90
+ def object_cache_include(*accessors)
91
+ self._object_cache_include << accessors
92
+ end
93
+
77
94
  def object_cache_on(*attrs)
78
95
  self._object_cache_attr_mappings << attrs
79
96
  define_singleton_method("find_by_#{attrs.join('_and_')}") do |*args|
@@ -1,5 +1,5 @@
1
1
  module Cache
2
2
  module Object
3
- VERSION = "0.0.7"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -108,5 +108,13 @@ RSpec.describe "Caching" do
108
108
  end
109
109
  end
110
110
 
111
+ describe "object_cache_include" do
112
+ it "has expected additional attribute" do
113
+ user.shoe_size = '14'
114
+ user.write_cache!
115
+
116
+ expect(User.find(user.id).shoe_size).to eq('14')
117
+ end
118
+ end
111
119
 
112
120
  end
@@ -40,6 +40,9 @@ end
40
40
  class User < ActiveRecord::Base
41
41
  include Cache::Object::ActiveRecord
42
42
 
43
+ object_cache_include :shoe_size
44
+ attr_accessor :shoe_size
45
+
43
46
  object_cache_on :name, :age
44
47
 
45
48
  after_save :asplode_if_name_is_asplode
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cache-object
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Camuto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-25 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,6 +17,9 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '3.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4.2'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -24,6 +27,9 @@ dependencies:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4.2'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: ruby-usdt
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -213,7 +219,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
219
  version: '0'
214
220
  requirements: []
215
221
  rubyforge_project:
216
- rubygems_version: 2.2.0
222
+ rubygems_version: 2.2.3
217
223
  signing_key:
218
224
  specification_version: 4
219
225
  summary: Object R/W Caching
@@ -227,4 +233,3 @@ test_files:
227
233
  - spec/features/cache_object_spec.rb
228
234
  - spec/spec_helper.rb
229
235
  - spec/support/models.rb
230
- has_rdoc: