entity_store 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/entity_store.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module EntityStore
2
2
  require 'logger'
3
+ require 'entity_store/cache'
3
4
  require 'entity_store/entity'
4
5
  require 'entity_store/entity_value'
5
6
  require 'entity_store/event'
@@ -17,6 +18,9 @@ module EntityStore
17
18
  yield self
18
19
  end
19
20
 
21
+ # Public: cache to be used
22
+ attr_accessor :cache
23
+
20
24
  def connection_profile
21
25
  @_connection_profile
22
26
  end
@@ -0,0 +1,15 @@
1
+ # Private: wrapper for the assigned cache
2
+ #
3
+ module EntityStore
4
+ module Cache
5
+
6
+ def cache_enabled?
7
+ EntityStore.cache
8
+ end
9
+
10
+ def cache_fetch(id, version)
11
+ cache_enabled? ? EntityStore.cache.fetch("_entity_store_#{id}_#{version}") { yield } : yield
12
+ end
13
+ end
14
+
15
+ end
@@ -4,6 +4,7 @@ require 'uri'
4
4
  module EntityStore
5
5
  class MongoEntityStore
6
6
  include Mongo
7
+ include Cache
7
8
 
8
9
  def open_connection
9
10
  @db ||= open_store
@@ -73,14 +74,16 @@ module EntityStore
73
74
  if attrs = entities.find_one('_id' => BSON::ObjectId.from_string(id))
74
75
  entity = get_type_constant(attrs['_type']).new(attrs['snapshot'] || {'id' => id, 'version' => attrs['version']})
75
76
 
76
- since_version = attrs['snapshot'] ? attrs['snapshot']['version'] : nil
77
+ cache_fetch(id, entity.version) {
78
+ since_version = attrs['snapshot'] ? attrs['snapshot']['version'] : nil
77
79
 
78
- get_events(id, since_version).each do |e|
79
- e.apply(entity)
80
- entity.version = e.entity_version
81
- end
80
+ get_events(id, since_version).each do |e|
81
+ e.apply(entity)
82
+ entity.version = e.entity_version
83
+ end
82
84
 
83
- entity
85
+ entity
86
+ }
84
87
  else
85
88
  raise NotFound.new(id) if raise_exception
86
89
  nil
@@ -1,3 +1,3 @@
1
1
  module EntityStore
2
- VERSION = "0.0.8".freeze
2
+ VERSION = "0.0.9".freeze
3
3
  end
@@ -29,7 +29,7 @@ describe MongoEntityStore do
29
29
  '_type' => "MongoEntityStoreSpec::DummyEntity",
30
30
  'version' => @version = random_integer
31
31
  }
32
- @entity = MongoEntityStoreSpec::DummyEntity.new
32
+ @entity = MongoEntityStoreSpec::DummyEntity.new(:id => @id, :version => @version)
33
33
  MongoEntityStoreSpec::DummyEntity.stub(:new) { @entity }
34
34
  @entities_collection = mock('MongoCollection', :find_one => @attrs)
35
35
  @store.stub(:entities) { @entities_collection }
@@ -61,6 +61,13 @@ describe MongoEntityStore do
61
61
  subject
62
62
  @entity.version.should eq(@events.last.entity_version)
63
63
  end
64
+ it "should attempt to retrieve from the cache" do
65
+ @store.should_receive(:cache_fetch).with(@id, @version) { @entity }
66
+ subject
67
+ end
68
+ it "should return the entity" do
69
+ subject.should eq(@entity)
70
+ end
64
71
  context "when a snapshot exists" do
65
72
  before(:each) do
66
73
  @attrs['snapshot'] = {
@@ -77,6 +84,15 @@ describe MongoEntityStore do
77
84
  subject
78
85
  end
79
86
  end
87
+ context "when item exists in cache" do
88
+ before(:each) do
89
+ @another_entity = MongoEntityStoreSpec::DummyEntity.new
90
+ @store.stub(:cache_fetch) { @another_entity }
91
+ end
92
+ it "should reutrn the other entity" do
93
+ subject.should eq(@another_entity)
94
+ end
95
+ end
80
96
  end
81
97
 
82
98
  describe "#get_entity!" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entity_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -50,6 +50,7 @@ extensions: []
50
50
  extra_rdoc_files: []
51
51
  files:
52
52
  - lib/entity_store/attributes.rb
53
+ - lib/entity_store/cache.rb
53
54
  - lib/entity_store/config.rb
54
55
  - lib/entity_store/entity.rb
55
56
  - lib/entity_store/entity_value.rb