Permalink
Browse files

Immutable Objects and Inc/Dec native

  • Loading branch information...
1 parent b18499d commit 5f8c8c3782d0266a1755d001459e7ec369fde717 @thiagopradi thiagopradi committed Jul 2, 2009
Showing with 56 additions and 37 deletions.
  1. +6 −7 lib/tokyo_store.rb
  2. +20 −0 spec/tokyo_store_rufus_spec.rb
  3. +30 −30 spec/tokyo_store_spec.rb
View
@@ -57,7 +57,7 @@ def read_multi(*keys)
def read(key, options = nil) # :nodoc:
super
- @data[key] ? Marshal.load(@data[key]) : nil
+ @data[key] ? Marshal.load(@data[key]).freeze : nil
# if str = @data.get(key)
# Marshal.load str
# else
@@ -100,18 +100,17 @@ def exist?(key, options = nil) # :nodoc:
end
def increment(key, amount = 1) # :nodoc:
- write(key, read(key) + amount)
- # TODO native?
+ #NATIVE, JUST SEE ABOUT MARSHAL
+ @data.incr(key, amount)
end
def decrement(key, amount = 1) # :nodoc:
- write(key, read(key) - amount)
- # log("decrement", key, amount)
- #TODO native?
+ # WARNING! NATIVE, BUT UGLY
+ @data.incr(key, -amount)
end
def delete_matched(matcher, options = nil) # :nodoc:
- #TODO - substring matching ?
+ #TODO
end
def clear
@@ -40,6 +40,11 @@
@cache.read('foo').should eql([1,2,3])
end
+ it "should write integers" do
+ @cache.write('foo', 1)
+ @cache.read('foo').should eql(1)
+ end
+
it "should read and write obj" do
obj = City.new; obj.name = "Acapulco"; obj.pop = 717766
@cache.write('foo', obj)
@@ -87,6 +92,21 @@
@cache.stats.should match(hash_including({ :type => "hash"}))
end
+ it "store objects should be immutable should be immutable" do
+ @cache.write('foo', 'bar')
+ lambda { @cache.read('foo').gsub!(/.*/, 'baz') }.should raise_error(ActiveSupport::FrozenObjectError)
+ @cache.read('foo').should == 'bar'
+ end
+
+ it "should delete matched" do
+ @cache.write("val", 1)
+ @cache.write("value", 1)
+ @cache.write("not", 1)
+ @cache.delete_matched('val')
+
+ @cach
+ end
+
after(:all) do
@cache.clear
end
@@ -1,43 +1,43 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
if ENV['CABINET']
-describe "TokyoStore" do
- it "should store fragment cache" do
- HDB.should_receive(:new).and_return(@mock_hdb = mock("HDB"))
+ describe "TokyoStore" do
+ it "should store fragment cache" do
+ HDB.should_receive(:new).and_return(@mock_hdb = mock("HDB"))
@mock_hdb.should_receive(:open).with('data.tch', 6).and_return(true)
- store = ActiveSupport::Cache.lookup_store :tokyo_store, "data.tch"
- store.should be_kind_of ActiveSupport::Cache::TokyoStore
- end
+ store = ActiveSupport::Cache.lookup_store :tokyo_store, "data.tch"
+ store.should be_kind_of ActiveSupport::Cache::TokyoStore
+ end
- it "should fail" do
- tokyo = HDB.new
- tokyo.open('data.tch')
- HDB.should_not_receive(:new)
- store = ActiveSupport::Cache.lookup_store :tokyo_store, tokyo
- store.should be_kind_of ActiveSupport::Cache::TokyoStore
- end
+ it "should fail" do
+ tokyo = HDB.new
+ tokyo.open('data.tch')
+ HDB.should_not_receive(:new)
+ store = ActiveSupport::Cache.lookup_store :tokyo_store, tokyo
+ store.should be_kind_of ActiveSupport::Cache::TokyoStore
+ end
- describe "Similar" do
+ describe "Similar" do
- before(:all) do
- @cache = ActiveSupport::Cache::TokyoStore.new 'data.tcb'
- end
+ before(:all) do
+ @cache = ActiveSupport::Cache::TokyoStore.new 'data.tcb'
+ end
- it "test_should_read_and_write_strings" do
- @cache.write('foo', 'bar')
- @cache.read('foo').should eql('bar')
- end
+ it "test_should_read_and_write_strings" do
+ @cache.write('foo', 'bar')
+ @cache.read('foo').should eql('bar')
+ end
- it "test_should_read_and_write_hash" do
- @cache.write('foo', {:a => "b"})
- @cache.read('foo').should eql({:a => "b"})
- end
+ it "test_should_read_and_write_hash" do
+ @cache.write('foo', {:a => "b"})
+ @cache.read('foo').should eql({:a => "b"})
+ end
- it "test_should_read_and_write_hash" do
- @cache.write('foo', {:a => "b", :c => "d"})
- @cache.read('foo').should eql({:a => "b", :c => "d"})
+ it "test_should_read_and_write_hash" do
+ @cache.write('foo', {:a => "b", :c => "d"})
+ @cache.read('foo').should eql({:a => "b", :c => "d"})
+ end
end
- end
-end
+ end
end

0 comments on commit 5f8c8c3

Please sign in to comment.