Class: Pogocache::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/pogocache-ruby/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Cache

Returns a new instance of Cache.



2
3
4
# File 'lib/pogocache-ruby/cache.rb', line 2

def initialize(options = {})
  @extension = Pogocache::Extension.new(Pogocache.configuration.default_opts.merge(options))
end

Instance Method Details

#[](key) ⇒ Object



23
24
25
# File 'lib/pogocache-ruby/cache.rb', line 23

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



27
28
29
# File 'lib/pogocache-ruby/cache.rb', line 27

def []=(key, value)
  set(key, value)
end

#bytesizeObject



64
# File 'lib/pogocache-ruby/cache.rb', line 64

def bytesize = @extension.size

#cleanup(options = {}) ⇒ Object



86
# File 'lib/pogocache-ruby/cache.rb', line 86

def cleanup(options = {}) = @extension.sweep

#clearObject



66
# File 'lib/pogocache-ruby/cache.rb', line 66

def clear = @extension.clear

#countObject



60
# File 'lib/pogocache-ruby/cache.rb', line 60

def count = @extension.count

#decrement(key) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/pogocache-ruby/cache.rb', line 49

def decrement(key)
  value = get(key)&.to_i
  if value
    set(key, value - 1)
    value - 1
  else
    set(key, 1)
    1
  end
end

#delete(key) ⇒ Object



21
# File 'lib/pogocache-ruby/cache.rb', line 21

def delete(key) = @extension.delete(encode(key))

#delete_matched(matcher, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/pogocache-ruby/cache.rb', line 95

def delete_matched(matcher, options = {})
  @extension.each do |e|
    if decode(e[:key]).to_s.match(matcher)
      Pogocache::ITER_DELETE # standard:disable Lint/Void
    else
      Pogocache::ITER_CONTINUE # standard:disable Lint/Void
    end
  end
end

#each(opts = {}, &block) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pogocache-ruby/cache.rb', line 70

def each(opts = {}, &block)
  if block_given?
    retval = []
    @extension.each(opts) do
      retval << yield(decode(it[:key]), decode(it[:value]))

      Pogocache::ITER_CONTINUE # standard:disable Lint/Void
    end
    retval
  else
    @extension.each(opts)
  end
end

#entry(key) ⇒ Object



14
15
16
17
18
19
# File 'lib/pogocache-ruby/cache.rb', line 14

def entry(key)
  @extension.load(encode(key))&.tap do
    it[:key] = decode(it[:key])
    it[:value] = decode(it[:value])
  end
end

#fetch(key, &block) ⇒ Object



31
32
33
34
35
36
# File 'lib/pogocache-ruby/cache.rb', line 31

def fetch(key, &block)
  value = get(key)
  return value if value

  block&.call
end

#get(key) ⇒ Object



10
11
12
# File 'lib/pogocache-ruby/cache.rb', line 10

def get(key)
  decode(@extension.load(encode(key))&.dig(:value))
end

#increment(key) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/pogocache-ruby/cache.rb', line 38

def increment(key)
  value = get(key)&.to_i
  if value
    set(key, value + 1)
    value + 1
  else
    set(key, 1)
    1
  end
end

#nshardsObject



84
# File 'lib/pogocache-ruby/cache.rb', line 84

def nshards = @extension.nshards

#prune(options = {}) ⇒ Object



88
89
# File 'lib/pogocache-ruby/cache.rb', line 88

def prune(options = {})
end

#pruning?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/pogocache-ruby/cache.rb', line 91

def pruning?
  false
end

#set(key, value, options = {}) ⇒ Object



6
7
8
# File 'lib/pogocache-ruby/cache.rb', line 6

def set(key, value, options = {})
  @extension.store(encode(key), encode(value), options)
end

#sizeObject



62
# File 'lib/pogocache-ruby/cache.rb', line 62

def size = count

#sweepObject



68
# File 'lib/pogocache-ruby/cache.rb', line 68

def sweep = @extension.sweep

#synchronizeObject



105
106
# File 'lib/pogocache-ruby/cache.rb', line 105

def synchronize
end