Skip to content

Commit

Permalink
Replace MemoryStore mutex with a monitor to avoid issues with nested …
Browse files Browse the repository at this point in the history
…calls
  • Loading branch information
josh committed Aug 19, 2008
1 parent cd8e653 commit a4da817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
36 changes: 24 additions & 12 deletions activesupport/lib/active_support/cache/memory_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,63 @@ module Cache
class MemoryStore < Store
def initialize
@data = {}
@mutex = Mutex.new
@guard = Monitor.new
end

def fetch(key, options = {})
@mutex.synchronize do
@guard.synchronize do
super
end
end

def read(name, options = nil)
super
@data[name]
@guard.synchronize do
super
@data[name]
end
end

def write(name, value, options = nil)
super
@data[name] = value
@guard.synchronize do
super
@data[name] = value
end
end

def delete(name, options = nil)
@data.delete(name)
@guard.synchronize do
@data.delete(name)
end
end

def delete_matched(matcher, options = nil)
@data.delete_if { |k,v| k =~ matcher }
@guard.synchronize do
@data.delete_if { |k,v| k =~ matcher }
end
end

def exist?(name,options = nil)
@data.has_key?(name)
@guard.synchronize do
@data.has_key?(name)
end
end

def increment(key, amount = 1)
@mutex.synchronize do
@guard.synchronize do
super
end
end

def decrement(key, amount = 1)
@mutex.synchronize do
@guard.synchronize do
super
end
end

def clear
@data.clear
@guard.synchronize do
@data.clear
end
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions activesupport/test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,27 @@ def test_should_read_and_write_nil
File.delete("foo.cache")
end
end

class MemoryStoreTest < Test::Unit::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:memory_store)
end

def test_should_read_and_write
@cache.write('foo', 'bar')
assert_equal 'bar', @cache.read('foo')
end

def test_fetch_without_cache_miss
@cache.write('foo', 'bar')
assert_equal 'bar', @cache.fetch('foo') { 'baz' }
end

def test_fetch_with_cache_miss
assert_equal 'baz', @cache.fetch('foo') { 'baz' }
end

def test_fetch_with_forced_cache_miss
@cache.fetch('foo', :force => true) { 'bar' }
end
end

0 comments on commit a4da817

Please sign in to comment.