File: cache.rb

package info (click to toggle)
ruby-moneta 1.6.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,776 kB
  • sloc: ruby: 13,201; sh: 178; makefile: 7
file content (119 lines) | stat: -rw-r--r-- 3,111 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
module Moneta
  # Combines two stores. One is used as cache, the other as backend adapter.
  #
  # @example Add `Moneta::Cache` to proxy stack
  #   Moneta.build do
  #     use(:Cache) do
  #      adapter { adapter :File, dir: 'data' }
  #      cache { adapter :Memory }
  #     end
  #   end
  #
  # @api public
  class Cache
    include Defaults

    # @api private
    class DSL
      def initialize(store, &block)
        @store = store
        instance_eval(&block)
      end

      # @api public
      def adapter(store = nil, &block)
        raise 'Adapter already set' if @store.adapter
        raise ArgumentError, 'Only argument or block allowed' if store && block
        @store.adapter = store || Moneta.build(&block)
      end

      # @api public
      def cache(store = nil, &block)
        raise 'Cache already set' if @store.cache
        raise ArgumentError, 'Only argument or block allowed' if store && block
        @store.cache = store || Moneta.build(&block)
      end
    end

    attr_accessor :cache, :adapter

    # @param [Hash] options Options hash
    # @option options [Moneta store] :cache Moneta store used as cache
    # @option options [Moneta store] :adapter Moneta store used as adapter
    # @yieldparam Builder block
    def initialize(options = {}, &block)
      @cache, @adapter = options[:cache], options[:adapter]
      DSL.new(self, &block) if block_given?
    end

    # (see Proxy#key?)
    def key?(key, options = {})
      @cache.key?(key, options) || @adapter.key?(key, options)
    end

    # (see Proxy#load)
    def load(key, options = {})
      if options[:sync] || (value = @cache.load(key, options)) == nil
        value = @adapter.load(key, options)
        @cache.store(key, value, options) if value != nil
      end
      value
    end

    # (see Proxy#store)
    def store(key, value, options = {})
      @cache.store(key, value, options)
      @adapter.store(key, value, options)
    end

    # (see Proxy#increment)
    def increment(key, amount = 1, options = {})
      @cache.delete(key, options)
      @adapter.increment(key, amount, options)
    end

    # (see Proxy#create)
    def create(key, value, options = {})
      if @adapter.create(key, value, options)
        @cache.store(key, value, options)
        true
      else
        false
      end
    end

    # (see Proxy#delete)
    def delete(key, options = {})
      @cache.delete(key, options)
      @adapter.delete(key, options)
    end

    # (see Proxy#clear)
    def clear(options = {})
      @cache.clear(options)
      @adapter.clear(options)
      self
    end

    # (see Proxy#close)
    def close
      @cache.close
      @adapter.close
    end

    # (see Proxy#each_key)
    def each_key(&block)
      raise NotImplementedError, 'adapter doesn\'t support #each_key' \
        unless supports? :each_key

      return enum_for(:each_key) unless block_given?
      @adapter.each_key(&block)
      self
    end

    # (see Proxy#features)
    def features
      @features ||= ((@cache.features + [:create, :increment, :each_key]) & @adapter.features).freeze
    end
  end
end