simple_file_cache 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/simple_file_cache.rb +81 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e05ca7a4e6938b947b6d2690e2198a506bccc3f
|
4
|
+
data.tar.gz: 65b21f3bfbb53ee063bfa17afd1761cab146334c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 29af265e9a56c67f903c8035d3f683f02d8876b7dd5fe8e40e6a64e1f10e168879276f6666e78024a064f68f59a1b6c1e5ecc93f3e23205d8ff74fb06949e0ea
|
7
|
+
data.tar.gz: 7d45afe398b05fe7f08dfa1fb3a9adc9fdd08d02206caa01436852ffee7363e669c10ad3dfc9da42b0a8e8e2dc472e3854a20a6f03ea14fd8da5323376d329d5
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module SimpleFileCache
|
4
|
+
|
5
|
+
# Check whether a cache file exists and is recent (last modified today). If so,
|
6
|
+
# read the file using Marshal#load and return it. Otherwise, execute the given
|
7
|
+
# block to obtain the new data, save to the cache file using Marshal#dump and
|
8
|
+
# return the updated data.
|
9
|
+
def self.load_or_recompute(cache_file_name, &block)
|
10
|
+
if cache_file_name.include?('/')
|
11
|
+
cache_file_pathname = cache_file_name
|
12
|
+
else
|
13
|
+
cache_file_pathname = "#{configuration.cache_dir_path}/#{cache_file_name}"
|
14
|
+
end
|
15
|
+
|
16
|
+
cache_file_is_recent = file_is_recent?(cache_file_pathname)
|
17
|
+
rails_production_env = defined?(Rails) && Rails.env.production?
|
18
|
+
use_cached_copy = cache_file_is_recent && !rails_production_env
|
19
|
+
|
20
|
+
if use_cached_copy
|
21
|
+
log "File '#{cache_file_pathname}' exists and is recent. Using cached file."
|
22
|
+
cache_file_contents = File.binread(cache_file_pathname)
|
23
|
+
return Marshal.load(cache_file_contents)
|
24
|
+
else
|
25
|
+
log "File '#{cache_file_pathname}' inexistent or out of date. Creating new cache file."
|
26
|
+
data_to_cache = block.call
|
27
|
+
File.binwrite(cache_file_pathname, Marshal.dump(data_to_cache))
|
28
|
+
return data_to_cache
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
attr_accessor :configuration
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.configuration
|
37
|
+
@configuration ||= Configuration.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.reset
|
41
|
+
@configuration = Configuration.new
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.configure
|
45
|
+
yield(configuration)
|
46
|
+
end
|
47
|
+
|
48
|
+
class Configuration
|
49
|
+
attr_accessor :cache_dir_path, :cache_expiration_policy, :cache_max_age_in_seconds
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@cache_dir_path = '.'
|
53
|
+
@cache_expiration_policy = :yesterday_or_earlier # :max_age
|
54
|
+
@cache_max_age_in_seconds = nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
# Allow the client code to silence the gem by setting an environment variable
|
61
|
+
def self.log(message)
|
62
|
+
return if ENV['TEST'] != ''
|
63
|
+
puts message
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.file_is_recent?(file_pathname)
|
67
|
+
return false unless File.exists?(file_pathname)
|
68
|
+
|
69
|
+
file_last_changed_at = File.mtime(file_pathname)
|
70
|
+
|
71
|
+
case configuration.cache_expiration_policy
|
72
|
+
when :yesterday_or_earlier
|
73
|
+
return (file_last_changed_at >= Date.today.to_time)
|
74
|
+
when :max_age
|
75
|
+
file_age = Time.now - file_last_changed_at
|
76
|
+
return (file_age <= configuration.cache_max_age_in_seconds)
|
77
|
+
else
|
78
|
+
return false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_file_cache
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ricardo Jasinski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: 'SimpleFileCache writes a ruby object to a binary file so that it can
|
42
|
+
be retrieved later from the disk rather than recomputed from scratch. It defines
|
43
|
+
a single method #load_or_recompute that receives a file path and a block. If the
|
44
|
+
file exists and is recent (last changed today), it returns the file contents read
|
45
|
+
with Marshal#load. Otherwise, it executesthe block, saves its return value with
|
46
|
+
Marshal#dump and returns the new data.'
|
47
|
+
email: jasinski@solvis.com.br
|
48
|
+
executables: []
|
49
|
+
extensions: []
|
50
|
+
extra_rdoc_files: []
|
51
|
+
files:
|
52
|
+
- lib/simple_file_cache.rb
|
53
|
+
homepage:
|
54
|
+
licenses: []
|
55
|
+
metadata: {}
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 2.5.1
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: SimpleFileCache writes a ruby object to a binary file so that it can be retrieved
|
76
|
+
later from the disk rather than recomputed from scratch.
|
77
|
+
test_files: []
|