class SieveCache::Cache(K, V)
- SieveCache::Cache(K, V)
- Reference
- Object
Included Modules
Defined in:
sieve_cache.crConstructors
-
.new(cap : UInt32)
Initialize cache with given capacity
Instance Method Summary
-
#clear
Clear cache
-
#del(key : K, &)
Delete value by key, call the block if key is not found
-
#get(key : K, &)
Get value by key, call the block if key is not found
-
#get_or_set(key : K, & : K -> V) : V
Get value by key, call the block to get value to set into cache if key is not found
-
#has?(key : K) : Bool
Check cache if has key?
-
#set(key : K, value : V)
Set key-value to cache
-
#to_s
to string
Instance methods inherited from module SieveCache::CacheBase(K, V)
[](key : K)
[],
[]=(key : K, value : V)
[]=,
[]?(key : K) : V | Nil
[]?,
cap : UInt32
cap,
clear
clear,
del(key : K, & : K -> )del(key : K) : V | Nil del, empty? : Bool empty?, full? : Bool full?, get(key : K, & : K -> )
get(key : K, fallback : V) : V get, get_or_set(key : K, & : K -> V) : V
get_or_set(key : K, value : V) : V get_or_set, has?(key : K) : Bool has?, len : UInt32 len, set(key : K, value : V) set
Constructor Detail
def self.new(cap : UInt32)
#
Initialize cache with given capacity
cache = Cache(String, String).new(10)
Instance Method Detail
def clear
#
Description copied from module SieveCache::CacheBase(K, V)
Clear cache
cache.clear
p! cache.len # => 0
def del(key : K, &)
#
Description copied from module SieveCache::CacheBase(K, V)
Delete value by key, call the block if key is not found
deleted_value = cache.del("foo") { nil }
def get(key : K, &)
#
Description copied from module SieveCache::CacheBase(K, V)
Get value by key, call the block if key is not found
value = cache.get("foo") { |k| raise "Key: #{k} not found" }
def get_or_set(key : K, & : K -> V) : V
#
Description copied from module SieveCache::CacheBase(K, V)
Get value by key, call the block to get value to set into cache if key is not found
value = cache.get_or_set("foo") { |k| "new value of key: #{k}" }
p! value # => "new value of key: foo"
p! cache["foo"] # => "new value of key: foo"
def has?(key : K) : Bool
#
Description copied from module SieveCache::CacheBase(K, V)
Check cache if has key?
if cache.has?("hello")
...
end
def set(key : K, value : V)
#
Description copied from module SieveCache::CacheBase(K, V)
Set key-value to cache
cache.set("foo", "bar")
cache.set("foo", "baz")