persistent-cache-storage-directory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4bca15b3f0dc16e0a56aec26b72b421c450da0c4
4
+ data.tar.gz: b759a45d7c38d4002a4f4618975dd00ca41001f3
5
+ SHA512:
6
+ metadata.gz: 836b2c093d4a0c91ba1be7ec05dbb891c3d306cd19f28f4389fcce184a18d360953b3fc6a556aa4cf9470e1ea91e345c6c060f4cc9f058ea933e9710031107a6
7
+ data.tar.gz: 5b9579ebd87dc7b9ba03b682f01c00e1bb9cbe68539956610f4447ae24a432a710bb619c61906002b64d2be1215aa35569d0ebdf07075744e7017700bce723a6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ persistent-cache-storage-directory
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.3.0
@@ -0,0 +1 @@
1
+ jruby-9.0.4.0
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p643
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in persistent-cache-storage-directory.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ernst Van Graan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Persistent::Cache::StorageDirectory
2
+
3
+ This gem provides a directory storage back-end to Persistent::Cache. Please see https://rubygems.org/gems/persistent-cache.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'persistent-cache-storage-directory'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install persistent-cache-storage-directory
20
+
21
+ ## Usage
22
+
23
+ Keys are required to be strings that are valid for use as directory names. The cache then stores from a storage root (configured in the StorageDirector constructor) with a subdirectory for each key, and a file called 'cache' for the value. The first line in the cache file is the timestamp of the entry.
24
+
25
+ When a StorageDirectory is used, it can be asked whether a key is present and what the path to a cache value is using:
26
+
27
+ get_value_path(key)
28
+
29
+ key_cached?(key)
30
+
31
+ Tell Persistent::Cache to use this provider so:
32
+
33
+ require 'persistent-cache/storage_directory'
34
+ require 'persistent-cache'
35
+
36
+ storage_location = "/tmp/directory-cache"
37
+ freshness = nil # forever or freshness in seconds
38
+ cache = Persistent::Cache.new(storage_location, freshness, Persistent::Cache::STORAGE_DIRECTORY)
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+
44
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+
46
+ ## Contributing
47
+
48
+ Bug reports and pull requests are welcome on GitHub at https://github.com/evangraan/persistent-cache-storage-directory. This gem was sponsored by Hetzner (Pty) Ltd - http://hetzner.co.za
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
53
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "persistent/cache/storage/directory"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,169 @@
1
+ require 'persistent-cache/storage_api'
2
+ require 'persistent-cache/version'
3
+
4
+ module Persistent
5
+ class StorageDirectory < Persistent::Cache::StorageApi::API
6
+ CACHE_FILE = "cache.gz" unless defined? CACHE_FILE; CACHE_FILE.freeze
7
+
8
+ attr_accessor :storage_root
9
+
10
+ def initialize(storage_details)
11
+ raise ArgumentError.new("Storage details not provided") if storage_details.nil? or storage_details == ""
12
+ @storage_root = storage_details
13
+ connect_to_database
14
+ end
15
+
16
+ def connect_to_database
17
+ FileUtils.makedirs([@storage_root]) if not File.directory?(@storage_root)
18
+ end
19
+
20
+ def save_key_value_pair(key, value, timestamp = nil)
21
+ prepare_to_store_key_value(key, value, timestamp)
22
+ store_key_value(key, value, get_time(timestamp)) if not value.nil?
23
+ end
24
+
25
+ def lookup_key(key)
26
+ validate_key(key)
27
+ return [] if not File.exists? compile_value_path(key)
28
+ lookup_key_value_timestamp(key)
29
+ end
30
+
31
+ def delete_entry(key)
32
+ validate_key(key)
33
+ FileUtils.rm_rf(compile_key_path(key))
34
+ end
35
+
36
+ def size
37
+ count = Dir::glob("#{@storage_root}/**/#{CACHE_FILE}").size
38
+ end
39
+
40
+ def keys
41
+ return [] if size == 0
42
+ list_keys_sorted
43
+ end
44
+
45
+ def clear
46
+ keys.each do |key|
47
+ delete_entry(key[0])
48
+ end
49
+ end
50
+
51
+ def get_value_path(key)
52
+ validate_key(key)
53
+
54
+ return nil if not key_cached?(key)
55
+
56
+ compile_value_path(key)
57
+ end
58
+
59
+ def get_value_path_even_when_not_cached(key)
60
+ compile_value_path(key)
61
+ end
62
+
63
+ def key_cached?(key)
64
+ # don't read the value here, as it may be very large - rather look whether the key is present
65
+ File.exists? compile_key_path(key)
66
+ end
67
+
68
+ private
69
+
70
+ def list_keys_sorted
71
+ result = []
72
+ append_keys(result).sort
73
+ end
74
+
75
+ def append_keys(result)
76
+ get_key_directories.each do |dir|
77
+ result << extract_key_from_directory(dir)
78
+ end
79
+ result
80
+ end
81
+
82
+ def get_key_directories
83
+ subdirectories = Dir::glob("#{@storage_root}/**/")
84
+ #exclude the storage root directory itself
85
+ subdirectories[1..-1]
86
+ end
87
+
88
+ def extract_key_from_directory(dir)
89
+ key = dir.match(/#{@storage_root}\/(\w+)\//)[1]
90
+ [key]
91
+ end
92
+
93
+ def compile_key_path(key)
94
+ "#{@storage_root}/#{key}"
95
+ end
96
+
97
+ def compile_value_path(key)
98
+ "#{compile_key_path(key)}/#{CACHE_FILE}"
99
+ end
100
+
101
+ def validate_save_key_value_pair(key, value)
102
+ validate_key(key)
103
+ raise ArgumentError.new("Only string values allowed") if not value.is_a?(String)
104
+ end
105
+
106
+ def lookup_key_value_timestamp(key)
107
+ result = [[],[]]
108
+ data = File.read(compile_value_path(key))
109
+ format_value_timestamp(result, data)
110
+ end
111
+
112
+ def format_value_timestamp(result, data)
113
+ result[0][0] = data.lines.to_a[1..-1].join
114
+ result[0][1] = data.lines.to_a[0..0].join.split("\n")[0]
115
+ result
116
+ end
117
+
118
+ def validate_key(key)
119
+ raise ArgumentError.new("Only string keys allowed") if not key.is_a?(String)
120
+ root_path = Pathname.new(File.absolute_path(@storage_root))
121
+ key_path = Pathname.new(File.absolute_path(compile_key_path(key)))
122
+ relative = key_path.relative_path_from(root_path).to_s
123
+ raise ArgumentError.new("key is outside of storage_root scope") if relative.start_with?("..")
124
+ raise ArgumentError.new("key is the same as storage_root") if relative == "."
125
+ end
126
+
127
+ def empty_key_value(key)
128
+ FileUtils.makedirs([compile_key_path(key)]) if not File.exists?(compile_key_path(key))
129
+ FileUtils.rm_f(compile_value_path(key))
130
+ end
131
+
132
+ def get_time(timestamp)
133
+ timestamp.nil? ? Time.now.to_s : timestamp.to_s
134
+ end
135
+
136
+ def prepare_to_store_key_value(key, value, timestamp)
137
+ validate_save_key_value_pair(key, value)
138
+ delete_entry(key)
139
+ empty_key_value(key)
140
+ end
141
+
142
+ def store_key_value(key, value, timestamp)
143
+ tempfile = Tempfile.new("store_key_value")
144
+ store_value_timestamp_in_file(value, timestamp, tempfile)
145
+ sync_cache_value(key, tempfile)
146
+ end
147
+
148
+ def sync_cache_value(key, tempfile)
149
+ target = compile_value_path(key)
150
+ FileUtils.rm_f(target)
151
+ FileUtils.move(tempfile.path, target)
152
+ end
153
+
154
+ def store_value_timestamp_in_file(value, timestamp, tempfile)
155
+ output = File.open(tempfile, 'a')
156
+ write_value_timestamp(output, value, timestamp)
157
+ output.close
158
+ end
159
+
160
+ def write_value_timestamp(output, value, timestamp)
161
+ # Have to be explicit about writing to the file. 'puts' collapses newline at the end of the file and
162
+ # so does not guarantee exact replication of 'value' on lookup
163
+ output.write(timestamp)
164
+ output.write("\n")
165
+ output.write(value)
166
+ output.flush
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,9 @@
1
+ module Persistent
2
+ module Cache
3
+ module Storage
4
+ module Directory
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'persistent-cache/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "persistent-cache-storage-directory"
8
+ spec.version = Persistent::Cache::Storage::Directory::VERSION
9
+ spec.authors = ["Ernst Van Graan"]
10
+ spec.email = ["ernst.van.graan@hetzner.co.za"]
11
+
12
+ spec.summary = %q{provides a directory storage back-end to Persistent::Cache.}
13
+ spec.description = %q{provides a directory storage back-end to Persistent::Cache.}
14
+ spec.homepage = "https://github.com/evangraan/persistent-cache-storage-directory"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_dependency "persistent-cache-storage-api"
26
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persistent-cache-storage-directory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ernst Van Graan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: persistent-cache-storage-api
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: provides a directory storage back-end to Persistent::Cache.
70
+ email:
71
+ - ernst.van.graan@hetzner.co.za
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-gemset"
79
+ - ".ruby-version"
80
+ - ".ruby-version-jruby"
81
+ - ".ruby-version-ruby"
82
+ - ".travis.yml"
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/persistent-cache/storage_directory.rb
90
+ - lib/persistent-cache/version.rb
91
+ - persistent-cache-storage-directory.gemspec
92
+ homepage: https://github.com/evangraan/persistent-cache-storage-directory
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: provides a directory storage back-end to Persistent::Cache.
116
+ test_files: []