lib/zache.rb in zache-0.5.2 vs lib/zache.rb in zache-0.5.3
- old
+ new
@@ -20,12 +20,10 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
-require 'monitor'
-
# It is a very simple thread-safe in-memory cache with an ability to expire
# keys automatically, when their lifetime is over. Use it like this:
#
# require 'zache'
# zache = Zache.new
@@ -48,11 +46,11 @@
# calculated result will be returned if it exists and is already expired.
def initialize(sync: true, dirty: false)
@hash = {}
@sync = sync
@dirty = dirty
- @monitor = Monitor.new
+ @mutex = Mutex.new
end
# Gets the value from the cache by the provided key. If the value is not
# found in the cache, it will be calculated via the provided block. If
# the block is not given, an exception will be raised. The lifetime
@@ -90,11 +88,11 @@
!rec.nil?
end
# Is cache currently locked doing something?
def locked?
- !@monitor.mon_try_enter { true }
+ @mutex.locked?
end
# Put a value into the cache.
def put(key, value, lifetime: 2**32)
synchronized do
@@ -137,10 +135,10 @@
@hash[key][:value]
end
def synchronized
if @sync
- @monitor.synchronize do
+ @mutex.synchronize do
# I don't know why, but if you remove this line, the tests will
# break. It seems to me that there is a bug in Ruby. Let's try to
# fix it or find a workaround and remove this line.
sleep 0.00001
yield