Class: Cachet::FileStore

Inherits:
Object
  • Object
show all
Defined in:
lib/cachet/file_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileStore

Returns a new instance of FileStore.



10
11
12
13
14
15
16
# File 'lib/cachet/file_store.rb', line 10

def initialize(root)
  @root = root
  FileUtils.mkdir_p root
  @optimize = TRUE
  @dir_level = 3
  @dir_count = 19
end

Instance Attribute Details

#dir_countObject

Returns the value of attribute dir_count.



8
9
10
# File 'lib/cachet/file_store.rb', line 8

def dir_count
  @dir_count
end

#dir_levelObject

Returns the value of attribute dir_level.



7
8
9
# File 'lib/cachet/file_store.rb', line 7

def dir_level
  @dir_level
end

#optimizeObject

Returns the value of attribute optimize.



6
7
8
# File 'lib/cachet/file_store.rb', line 6

def optimize
  @optimize
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/cachet/file_store.rb', line 5

def root
  @root
end

Instance Method Details

#entitiesObject



46
47
48
49
50
51
# File 'lib/cachet/file_store.rb', line 46

def entities
  Dir.entries(root).reject { |entry| entry =='.' || entry == '..' }.inject({}) do |entities, entity|
    entities[entity] = Dir["#{root}/#{entity}/**/*"].count { |file| File.file?(file) }
    entities
  end
end

#purge(entity, key) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/cachet/file_store.rb', line 36

def purge(entity, key)
  file_name = storage_path(entity, key)
  begin
    File.unlink file_name if  File.exist?(file_name)
  rescue
    Cachet.logger.warn "An element of #{entity} in the cache with key:#{key} to path #{file_name}entity has could not be deleted."
  end
  Cachet.logger.info "An element of #{entity} in the cache with key:#{key} to path #{file_name}entity has been removed from the cache."
end

#read(entity, key) ⇒ Object



18
19
20
21
# File 'lib/cachet/file_store.rb', line 18

def read(entity, key)
  file_name = storage_path(entity, key)
  File.open(file_name, 'rb') { |f| Marshal.load f.read } if  File.exist?(file_name)
end

#remove_entity(entity) ⇒ Object



53
54
55
# File 'lib/cachet/file_store.rb', line 53

def remove_entity(entity)
  FileUtils.remove_dir File.join root, entity
end

#storage_path(entity, key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cachet/file_store.rb', line 57

def storage_path(entity, key)
  file_name = Digest::MD5.hexdigest(key)
  if optimize
    dirs = (1..dir_level).inject({:hash=>key.sum, :dirs=>[]}) do |result, level|
      result[:dirs] << (result[:hash] % dir_count).abs.to_s
      result[:hash] += (result[:hash] * 31)
      result
    end
    file_name = File.join dirs[:dirs], file_name
  end
  File.join root, entity.to_s, file_name
end

#write(entity, key, data_to_be_stored) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cachet/file_store.rb', line 23

def write(entity, key, data_to_be_stored)
  file_name = storage_path(entity, key)
  temp_file_name = file_name + '.' + Thread.current.object_id.to_s
  FileUtils.mkdir_p File.dirname(file_name)
  File.open(temp_file_name, 'wb') { |f| f.write(Marshal.dump(data_to_be_stored)) }
  if File.exist?(file_name)
    File.unlink temp_file_name
  else
    FileUtils.mv temp_file_name, file_name
  end
  Cachet.logger.info "A new #{entity} entity stored in the cache with key:#{key} to path #{file_name}."
end