class SieveCache::Cache(K, V)

Included Modules

Defined in:

sieve_cache.cr

Constructors

Instance Method Summary

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)

[View source]

Instance Method Detail

def clear #
Description copied from module SieveCache::CacheBase(K, V)

Clear cache

cache.clear
p! cache.len # => 0

[View source]
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 }

[View source]
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" }

[View source]
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"

[View source]
def has?(key : K) : Bool #
Description copied from module SieveCache::CacheBase(K, V)

Check cache if has key?

if cache.has?("hello")
  ...
end

[View source]
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")

[View source]
def to_s #

to string

cache = Cache(String, String).new(10)
pp! cache.to_s

[View source]