File: file_based.rb

package info (click to toggle)
libinnate-ruby 2010.07-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 812 kB
  • ctags: 621
  • sloc: ruby: 4,242; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,209 bytes parent folder | download | duplicates (3)
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
module Innate
  class Cache

    # Used by caches that serialize their contents to the filesystem.
    # Right now we do not lock around write access to the file outside of the
    # process, that means that all FileBased caches are not safe for use if you
    # need more than one instance of your application.
    module FileBased
      attr_reader :filename

      def cache_setup(*args)
        @prefix = args.compact.join('-')

        @dir = File.join(Dir.tmpdir, self.class::DIR)
        FileUtils.mkdir_p(@dir)

        @filename = File.join(@dir, @prefix + self.class::EXT)
        @store = self.class::STORE.new(@filename)
      end

      def cache_clear
        FileUtils.mkdir_p(@dir)
        FileUtils.rm_f(@filename)
        @store = self.class::STORE.new(@filename)
      end

      def cache_store(*args)
        super{|key, value| transaction{|store| store[key] = value } }
      end

      def cache_fetch(*args)
        super{|key| transaction{|store| store[key] } }
      end

      def cache_delete(*args)
        super{|key| transaction{|store| store.delete(key) } }
      end

      def transaction(&block)
        Innate.sync{ @store.transaction(&block) }
      end
    end
  end
end