Class: ConvenientService::Support::Cache::Entities::Caches::Hash

Inherits:
Base
  • Object
show all
Defined in:
lib/convenient_service/support/cache/entities/caches/hash.rb

Direct Known Subclasses

ThreadSafeHash

Instance Attribute Summary

Attributes inherited from Base

#default, #key, #parent, #store

Instance Method Summary collapse

Methods inherited from Base

#==, #[], #[]=, #get, keygen, #keygen, #set

Methods included from AbstractMethod

abstract_method

Constructor Details

#initialize(store: {}, **kwargs) ⇒ void

Parameters:

  • store (Hash{Object => Object}) (defaults to: {})


18
19
20
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 18

def initialize(store: {}, **kwargs)
  super(store: ::Hash.new(kwargs[:default]).update(store), **kwargs)
end

Instance Method Details

#backendSymbol

Returns:

  • (Symbol)


25
26
27
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 25

def backend
  Constants::Backends::HASH
end

#clearConvenientService::Support::Cache::Entities::Caches::Hash



109
110
111
112
113
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 109

def clear
  store.clear

  self
end

#default=(value) ⇒ Object

Returns Can be any type.

Parameters:

  • value (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



149
150
151
152
153
154
155
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 149

def default=(value)
  save_self_as_scope_in_parent!

  store.default = value

  @default = value
end

#delete(key) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



98
99
100
101
102
103
104
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 98

def delete(key)
  value = store.delete(key)

  delete_self_as_scope_in_parent! if store.empty?

  value
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 35

def empty?
  store.empty?
end

#exist?(key) ⇒ Boolean

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Boolean)


46
47
48
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 46

def exist?(key)
  store.has_key?(key)
end

#fetch(key, &block) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.



83
84
85
86
87
88
89
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 83

def fetch(key, &block)
  return store[key] unless block

  save_self_as_scope_in_parent!

  store.fetch(key) { store[key] = yield }
end

#read(key) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



57
58
59
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 57

def read(key)
  store[key]
end

#scope(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base

Creates a scoped cache. Parent cache is modified on the first write to the scoped cache.

Parameters:

  • key (Object)

    Can be any type.

  • backed_by (Symbol) (defaults to: backend)
  • default (Object) (defaults to: self.default)

    Can be any type.

Returns:



126
127
128
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 126

def scope(key, backed_by: backend, default: self.default)
  store.fetch(key) { Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) }
end

#scope!(key, backed_by: backend, default: self.default) ⇒ ConvenientService::Support::Cache::Entities::Caches::Base

Creates a scoped cache. Parent cache is modified immediately.

Parameters:

  • key (Object)

    Can be any type.

  • backed_by (Symbol) (defaults to: backend)
  • default (Object) (defaults to: self.default)

    Can be any type.

Returns:



141
142
143
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 141

def scope!(key, backed_by: backend, default: self.default)
  store.fetch(key) { store[key] = Support::Cache.backed_by(backed_by).new(default: default, parent: self, key: key) }
end

#write(key, value) ⇒ Object

Returns Can be any type.

Parameters:

  • key (Object)

    Can be any type.

  • value (Object)

    Can be any type.

Returns:

  • (Object)

    Can be any type.



69
70
71
72
73
# File 'lib/convenient_service/support/cache/entities/caches/hash.rb', line 69

def write(key, value)
  save_self_as_scope_in_parent!

  store[key] = value
end