lib/zache.rb in zache-0.12.0 vs lib/zache.rb in zache-0.13.0

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true # (The MIT License) # -# Copyright (c) 2018 Yegor Bugayenko +# Copyright (c) 2018-2023 Yegor Bugayenko # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the 'Software'), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -32,11 +32,11 @@ # # For more information read # {README}[https://github.com/yegor256/zache/blob/master/README.md] file. # # Author:: Yegor Bugayenko (yegor256@gmail.com) -# Copyright:: Copyright (c) 2018 Yegor Bugayenko +# Copyright:: Copyright (c) 2018-2023 Yegor Bugayenko # License:: MIT class Zache # Fake implementation that doesn't cache anything, but behaves like it # does. It implements all methods of the original class, but doesn't do # any caching. This is very useful for testing. @@ -65,10 +65,11 @@ def clean; end end # Makes a new object of the cache. + # # "sync" is whether the hash is thread-safe (`true`) # or not (`false`); it is recommended to leave this parameter untouched, # unless you really know what you are doing. # # If the <tt>dirty</tt> argument is set to <tt>true</tt>, a previously @@ -83,25 +84,25 @@ # Total number of keys currently in cache. def size @hash.size end - # Gets the value from the cache by the provided key. If the value is not + # 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 (unless <tt>dirty</tt> # is set to <tt>true</tt>). The lifetime # must be in seconds. The default lifetime is huge, which means that the # key will never be expired. # # If the <tt>dirty</tt> argument is set to <tt>true</tt>, a previously # calculated result will be returned if it exists and is already expired. - def get(key, lifetime: 2**32, dirty: false) + def get(key, lifetime: 2**32, dirty: false, &block) if block_given? - if (dirty || @dirty) && locked? && expired?(key) && @hash.key?(key) - return @hash[key][:value] - end - synchronized { calc(key, lifetime) { yield } } + return @hash[key][:value] if (dirty || @dirty) && locked? && expired?(key) && @hash.key?(key) + synchronized { calc(key, lifetime, &block) } else rec = @hash[key] if expired?(key) return rec[:value] if dirty || @dirty @hash.delete(key) @@ -153,11 +154,11 @@ } end end # Removes the value from the hash, by the provied key. If the key is absent - # and the block is provide, the block will be called. + # and the block is provided, the block will be called. def remove(key) synchronized { @hash.delete(key) { yield if block_given? } } end # Remove all keys from the cache. @@ -166,10 +167,10 @@ end # Remove all keys that match the block. def remove_by synchronized do - @hash.keys.each do |k| + @hash.each_key do |k| @hash.delete(k) if yield(k) end end end