Class: Cachet::FileStore
- Inherits:
-
Object
- Object
- Cachet::FileStore
- Defined in:
- lib/cachet/file_store.rb
Instance Attribute Summary collapse
-
#dir_count ⇒ Object
Returns the value of attribute dir_count.
-
#dir_level ⇒ Object
Returns the value of attribute dir_level.
-
#optimize ⇒ Object
Returns the value of attribute optimize.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #entities ⇒ Object
-
#initialize(root) ⇒ FileStore
constructor
A new instance of FileStore.
- #purge(entity, key) ⇒ Object
- #read(entity, key) ⇒ Object
- #remove_entity(entity) ⇒ Object
- #storage_path(entity, key) ⇒ Object
- #write(entity, key, data_to_be_stored) ⇒ Object
Constructor Details
Instance Attribute Details
#dir_count ⇒ Object
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_level ⇒ Object
Returns the value of attribute dir_level.
7 8 9 |
# File 'lib/cachet/file_store.rb', line 7 def dir_level @dir_level end |
#optimize ⇒ Object
Returns the value of attribute optimize.
6 7 8 |
# File 'lib/cachet/file_store.rb', line 6 def optimize @optimize end |
#root ⇒ Object (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
#entities ⇒ Object
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 |