Class: LWAC::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/lwac/client/file_cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename, max_size = nil) ⇒ FileCache

Returns a new instance of FileCache.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lwac/client/file_cache.rb', line 8

def initialize(filename, max_size = nil)
  # thread safety
  @mutex = Mutex.new
  
  raise "No filename given" if filename == nil
  @filename = filename
  reset # pullup

  # index system for lookup
  @index = {}
  @orphan_keys = []

  # TODO: Max size in bytes
  # @max_filesize = max_size
end

Instance Method Details

#[](key) ⇒ Object

read a value



25
26
27
28
29
30
31
32
# File 'lib/lwac/client/file_cache.rb', line 25

def [](key)
  @mutex.synchronize{
    return if not @index.include?(key)

    @file.seek( @index[key][:start] )
    return Marshal.load( @file.read( @index[key][:len] ) )
  }
end

#[]=(key, value) ⇒ Object

Write a value



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lwac/client/file_cache.rb', line 35

def []=(key, value)
  @mutex.synchronize{
    # keep record of the old version if already a value
    delete_from_index(key) if @index[key]

    # Keep a note of where we're writing
    @index[key] = {:start => @end_of_file}

    # Write
    @file.seek(@end_of_file)
    @file.write( Marshal.dump(value) )
    @file.flush
    @end_of_file = @file.pos

    # then read off position as a length
    @index[key][:len] = @end_of_file - @index[key][:start]
  }
end

#closeObject

Close and remove file



132
133
134
135
136
137
# File 'lib/lwac/client/file_cache.rb', line 132

def close
  @mutex.synchronize{
    @file.close
    FileUtils.rm(@filename)
  }
end

#closed?Boolean

Currently closed?

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/lwac/client/file_cache.rb', line 140

def closed?
  @mutex.synchronize{
    @file.closed?
  }
end

#delete_from_index(key) ⇒ Object

Remove something from the index



67
68
69
70
71
# File 'lib/lwac/client/file_cache.rb', line 67

def delete_from_index(key)
  @mutex.synchronize{
    @orphan_keys << {:key => key, :value => @index.delete(key)} if @index.include?(key)
  }
end

#each_key(&block) ⇒ Object

Loop over each key



109
110
111
112
113
# File 'lib/lwac/client/file_cache.rb', line 109

def each_key(&block)
  @mutex.synchronize{
    @index.each_key{|k| yield(k) }
  }
end

#empty?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/lwac/client/file_cache.rb', line 122

def empty?
  length == 0
end

#filesizeObject

filesize in bytes



127
128
129
# File 'lib/lwac/client/file_cache.rb', line 127

def filesize
  @end_of_file
end

#flushObject

Flush to disk



102
103
104
105
106
# File 'lib/lwac/client/file_cache.rb', line 102

def flush 
  @mutex.synchronize{
    @file.flush
  }
end

#keysObject



73
74
75
76
77
# File 'lib/lwac/client/file_cache.rb', line 73

def keys
  @mutex.synchronize{
    @index.keys
  }
end

#lengthObject

How many items



116
117
118
119
120
# File 'lib/lwac/client/file_cache.rb', line 116

def length
  @mutex.synchronize{
    @index.length
  }
end

#orphan_keysObject

Read orphan keys norably non-unique.



81
82
83
84
85
# File 'lib/lwac/client/file_cache.rb', line 81

def orphan_keys
  @mutex.synchronize{
    @orphan_keys.map{|o| o[:key] }
  }
end

#syncObject

Status of sync mode



95
96
97
98
99
# File 'lib/lwac/client/file_cache.rb', line 95

def sync
  @mutex.synchronize{
    @file.sync
  }
end

#sync=(s) ⇒ Object

Enable sync mode



88
89
90
91
92
# File 'lib/lwac/client/file_cache.rb', line 88

def sync=(s)
  @mutex.synchronize{
    @file.sync = s
  }
end

#wipeObject Also known as: delete_all, reset

Wipe the store entirely



55
56
57
58
59
60
61
62
# File 'lib/lwac/client/file_cache.rb', line 55

def wipe
  @mutex.synchronize{
    @file.close if @file and not @file.closed?
    FileUtils.rm(@filename) if File.exist?(@filename)
    @file = File.open(@filename, 'wb+')
    @end_of_file = 0
  }
end