ultracache 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,7 +21,7 @@ module Ultracache
21
21
 
22
22
  included do
23
23
  extend ActiveModel::Callbacks
24
- define_model_callbacks :save, :destroy
24
+ define_model_callbacks :create, :update, :destroy
25
25
 
26
26
  after_create :save_cache
27
27
  after_update :update_cache
@@ -13,14 +13,13 @@ module Ultracache
13
13
  end
14
14
 
15
15
  def has_cached_queue(another, options={})
16
- name = options[:as] ? options[:as] : another
17
- relationship = HasCachedQueue.new name, :self_class => self,
16
+ relationship = HasCachedQueue.new another, :self_class => self,
18
17
  :associated_class => options[:class] ? options[:class] : another
19
18
 
20
19
  self.relationships(:strict => true).add(relationship)
21
20
 
22
- define_method name do |*options|
23
- read_cache(name, options.first || {})
21
+ define_method another do |*options|
22
+ read_cache(another, options.first || {})
24
23
  end
25
24
  end
26
25
 
@@ -28,7 +27,6 @@ module Ultracache
28
27
  relationship = HasCachedAttribute.new name, block, :self_class => self,
29
28
  :serializer => options[:serializer]
30
29
 
31
- rs = self.relationships(:strict => true)
32
30
  self.relationships(:strict => true).add(relationship)
33
31
 
34
32
  define_method name do |*options|
@@ -8,7 +8,7 @@ module Rails
8
8
  initializer 'ultracache' do |app|
9
9
  if defined? ::Mongoid
10
10
  require File.join(File.dirname(__FILE__), 'models/mongoid_extension.rb')
11
- ::Mongoid::Document.send :include, Ultracache::Models::MongoidExtension
11
+ ::Mongoid::Document.send :include, ::Ultracache::Models::MongoidExtension
12
12
  end
13
13
  end
14
14
  end
@@ -20,5 +20,14 @@ module Ultracache
20
20
  def save_cache(obj); end
21
21
  def destroy_cache(obj); end
22
22
  def update_cache(obj); end
23
+
24
+ protected
25
+ def storage
26
+ Ultracache::Configurations.storage
27
+ end
28
+
29
+ def serializer
30
+ Ultracache::Configurations.serializer
31
+ end
23
32
  end
24
33
  end
@@ -6,9 +6,6 @@ module Ultracache
6
6
  @alias = options[:as]
7
7
  @need_update = options[:need_update]
8
8
  @unless = options[:unless]
9
-
10
- @storage = Ultracache::Configurations.storage
11
- @serializer = Ultracache::Configurations.serializer
12
9
  end
13
10
 
14
11
  # Saves serialized form of object into cache queue which the object
@@ -24,10 +21,10 @@ module Ultracache
24
21
  value = if @serializer_block
25
22
  @serializer_block.call obj
26
23
  else
27
- JSON.generate(obj.as_json)
24
+ serializer.serialize(obj.as_json)
28
25
  end
29
26
 
30
- @storage.put_queue(key(obj), score_of(obj), value)
27
+ storage.put_queue(key(obj), score_of(obj), value)
31
28
  end
32
29
 
33
30
  # Destroys serialized cache from associated cache queue. In some cases
@@ -42,21 +39,21 @@ module Ultracache
42
39
  score = score_of(obj)
43
40
  key = key(obj)
44
41
 
45
- docs = @storage.get_queue(key, :from => score, :to => score)
42
+ docs = storage.get_queue(key, :from => score, :to => score)
46
43
 
47
44
  if docs.count == 1
48
45
  # Only one document is fetched from queue, and it is okay to remove
49
- @storage.remove_from_queue_by_range(key, :from => score, :to => score)
46
+ storage.remove_from_queue_by_range(key, :from => score, :to => score)
50
47
  elsif docs.count > 1
51
48
  # We should deserialize fetched documents to find the document having
52
49
  # the same id with `obj`
53
50
  docs.each do |doc|
54
- deserialized = @serializer.deserialize(doc)
51
+ deserialized = serializer.deserialize(doc)
55
52
  _id = deserialized["id"]
56
53
  _id = deserialized["_id"] unless _id
57
54
 
58
55
  if _id == obj.id
59
- @storage.remove_from_queue(key, doc)
56
+ storage.remove_from_queue(key, doc)
60
57
  end
61
58
  end
62
59
  end
@@ -84,13 +81,6 @@ module Ultracache
84
81
  def score_of(obj)
85
82
  obj.respond_to?(:cached_queue_score) ? obj.cached_queue_score : obj.id
86
83
  end
87
-
88
- def serialize(obj)
89
- # Is it okay to refer configurations object here?
90
- @serializer.serialize(obj) do |obj|
91
- @serializer_block.call(obj)
92
- end
93
- end
94
84
  end
95
85
  end
96
86
 
@@ -4,9 +4,6 @@ module Ultracache
4
4
  super(name, options)
5
5
  @serializer_method = options[:serializer]
6
6
  @serializing_block = block if block_given?
7
-
8
- @serializer = Ultracache::Configurations.serializer
9
- @storage = Ultracache::Configurations.storage
10
7
  end
11
8
 
12
9
  def key(obj)
@@ -18,14 +15,14 @@ module Ultracache
18
15
  # serializeing hash returned by its `as_json` method.
19
16
  def save_cache(obj)
20
17
  value = if @serializer_method
21
- @serializer.serialize(obj.send(@serializer_method))
18
+ serializer.serialize(obj.send(@serializer_method))
22
19
  elsif @serializing_block
23
- @serializer.serialize(@serializing_block.call(obj))
20
+ serializer.serialize(@serializing_block.call(obj))
24
21
  else
25
- @serializer.serialize(obj.as_json)
22
+ serializer.serialize(obj.as_json)
26
23
  end
27
24
 
28
- @storage.set(key(obj), value)
25
+ storage.set(key(obj), value)
29
26
  value
30
27
  end
31
28
 
@@ -34,12 +31,12 @@ module Ultracache
34
31
  def read_cache(obj, options = {})
35
32
  k = key(obj)
36
33
 
37
- @storage.get(k) || save_cache(obj)
34
+ storage.get(k) || save_cache(obj)
38
35
  end
39
36
 
40
37
  # Destroys cache from storage
41
38
  def destroy_cache(obj)
42
- @storage.del(key(obj))
39
+ storage.del(key(obj))
43
40
  end
44
41
 
45
42
  # Updates value of existing cache. Its behavior is same with that of
@@ -5,8 +5,6 @@ module Ultracache
5
5
  def initialize(name, options={})
6
6
  @fetch_by = options[:fetch_by]
7
7
  super(name, options)
8
-
9
- @storage = Ultracache::Configurations.storage
10
8
  end
11
9
 
12
10
  def key(obj)
@@ -20,11 +18,11 @@ module Ultracache
20
18
 
21
19
  fetch_by = @fetch_by || options[:fetch_by]
22
20
  if fetch_by && fetch_by == :rank
23
- @storage.get_queue_by_rank(k, options)
21
+ storage.get_queue_by_rank(k, options)
24
22
  elsif options[:per_page]
25
- @storage.get_queue_paged(k, options)
23
+ storage.get_queue_paged(k, options)
26
24
  else
27
- @storage.get_queue(k, options)
25
+ storage.get_queue(k, options)
28
26
  end
29
27
  end
30
28
  end
@@ -4,6 +4,7 @@ module Ultracache
4
4
  module Serializer
5
5
  class JsonSerializer < Serializer::Base
6
6
  def serialize(obj, &block)
7
+ obj = block.call(obj) if block_given?
7
8
  JSON.generate(obj)
8
9
  end
9
10
 
@@ -5,10 +5,8 @@ require 'json'
5
5
  module Ultracache
6
6
  module Storage
7
7
  class Redis
8
- include Storage
9
-
10
8
  def initialize(options = {})
11
- @urls = options[:urls] || 'localhost'
9
+ @urls = options[:urls] || ['redis://localhost:6379/1']
12
10
  end
13
11
 
14
12
  def connection
@@ -1,3 +1,3 @@
1
1
  module Ultracache
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -2,7 +2,13 @@ require "spec_helper"
2
2
 
3
3
  describe Ultracache::Cached do
4
4
  before do
5
- Ultracache::Configurations.storage = Ultracache::Storage::Redis.new(:urls => ['localhost:6379'])
5
+ Ultracache::Configurations.storage = Ultracache::Storage::Redis.new(:urls => ['redis://localhost:6379/1'])
6
+ Ultracache::Configurations.serializer = Ultracache::Serializer::JsonSerializer.new
7
+ end
8
+
9
+ after do
10
+ Ultracache::Configurations.storage = nil
11
+ Ultracache::Configurations.serializer = nil
6
12
  end
7
13
 
8
14
  context "without model hierarchy" do
@@ -2,10 +2,8 @@ require "spec_helper"
2
2
 
3
3
  describe Ultracache::BelongsAsCachedQueue do
4
4
  let(:belongs_as_cached_queue) do
5
- Ultracache::BelongsAsCachedQueue.new :cached_posts,
6
- :self_class => Post, :associated_class => Person do |p|
7
- p.to_json
8
- end
5
+ Ultracache::BelongsAsCachedQueue.new :cached_posts, nil,
6
+ :self_class => Post, :associated_class => Person
9
7
  end
10
8
 
11
9
  describe "#key" do
@@ -8,7 +8,7 @@ describe Ultracache::HasCachedAttribute do
8
8
  end
9
9
 
10
10
  let(:cached_attribute) do
11
- Ultracache::HasCachedAttribute.new :cached_name,
11
+ Ultracache::HasCachedAttribute.new :cached_name, nil,
12
12
  :self_class => Person, :serializer_block => person_attribute_block
13
13
  end
14
14
 
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  describe Ultracache::Serializer::JsonSerializer do
4
4
  let(:serializer) { Ultracache::Serializer::JsonSerializer.new }
5
- let(:obj) { Person.new }
5
+ let(:person) { Person.new }
6
6
 
7
7
  describe "#serialize" do
8
8
  it "serializes object to JSON" do
9
- serialized_str = serializer.serialize(obj) do
9
+ serialized_str = serializer.serialize(person) do |obj|
10
10
  { :id => obj.id }
11
11
  end
12
12
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ultracache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-07 00:00:00.000000000 Z
12
+ date: 2012-01-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
16
- requirement: &70152863679380 !ruby/object:Gem::Requirement
16
+ requirement: &70315745190060 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70152863679380
24
+ version_requirements: *70315745190060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &70152863677340 !ruby/object:Gem::Requirement
27
+ requirement: &70315745187660 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70152863677340
35
+ version_requirements: *70315745187660
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: redis
38
- requirement: &70152863675180 !ruby/object:Gem::Requirement
38
+ requirement: &70315749287900 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '2.2'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70152863675180
46
+ version_requirements: *70315749287900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: json
49
- requirement: &70152863673460 !ruby/object:Gem::Requirement
49
+ requirement: &70315749285640 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70152863673460
57
+ version_requirements: *70315749285640
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec
60
- requirement: &70152863669820 !ruby/object:Gem::Requirement
60
+ requirement: &70315749280800 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '2.6'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70152863669820
68
+ version_requirements: *70315749280800
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: mocha
71
- requirement: &70152863666360 !ruby/object:Gem::Requirement
71
+ requirement: &70315749278220 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70152863666360
79
+ version_requirements: *70315749278220
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bson_ext
82
- requirement: &70152863663540 !ruby/object:Gem::Requirement
82
+ requirement: &70315749272460 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70152863663540
90
+ version_requirements: *70315749272460
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: mongoid
93
- requirement: &70152863657880 !ruby/object:Gem::Requirement
93
+ requirement: &70315749268700 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70152863657880
101
+ version_requirements: *70315749268700
102
102
  description: Ultracache reduces computational costs occur from dynamic attributes
103
103
  by caching them into Redis
104
104
  email: