Class: Spider::Utils::FileSharedStore
- Inherits:
-
SharedStore
- Object
- SharedStore
- Spider::Utils::FileSharedStore
- Defined in:
- lib/spiderfw/utils/shared_store/file_shared_store.rb
Overview
Implementation of the SharedStore in the filesystem. This is a persistent store, accessible by different threads and processes at once.
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #delete(key) ⇒ Object
- #each_key ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(config = {}) ⇒ FileSharedStore
constructor
A new instance of FileSharedStore.
- #map_path(key) ⇒ Object
Methods inherited from SharedStore
Constructor Details
#initialize(config = {}) ⇒ FileSharedStore
Returns a new instance of FileSharedStore.
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 11 def initialize(config={}) super @path = config[:path] if (!@path && config[:name]) @path = Spider.conf.get('shared_store.file.base_path')+'/'+config[:name] end if (!@path) raise ArgumentError, "You must supply the FileSharedStore with a path, or a name a configured base path" end FileUtils.mkpath(@path) @sync = Sync.new end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 9 def path @path end |
Instance Method Details
#[](key) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 24 def [](key) path = map_path(key) if (File.exist?(path)) @sync.lock(Sync::SH) f = File.new(path, 'r') f.flock(File::LOCK_SH) data = Marshal.restore(f.read) f.flock(File::LOCK_UN) f.close @sync.lock(Sync::UN) end return data end |
#[]=(key, value) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 38 def []=(key, value) path = map_path(key) @sync.lock(Sync::EX) f = File.new(path, 'w') f.flock(File::LOCK_EX) f.puts(Marshal.dump(value)) f.flush f.flock(File::LOCK_UN) f.close @sync.lock(Sync::UN) end |
#delete(key) ⇒ Object
51 52 53 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 51 def delete(key) File.unlink(map_path(key)) end |
#each_key ⇒ Object
63 64 65 66 67 68 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 63 def each_key Dir.new(@path).each do |key| next unless File.file?(@path+'/'+key) yield key end end |
#include?(key) ⇒ Boolean
55 56 57 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 55 def include?(key) File.exist?(map_path(key)) end |
#map_path(key) ⇒ Object
59 60 61 |
# File 'lib/spiderfw/utils/shared_store/file_shared_store.rb', line 59 def map_path(key) "#{@path}/key" end |