filecache 1.0.1 → 1.0.3
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 +5 -5
- data/lib/filecache.rb +17 -17
- data/tests/{test_filecache.rb → filecache_test.rb} +20 -5
- metadata +75 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 828ce5557d7b60a0deab1418bff56079058fbe6cd2247b50425e0b0b0116b556
|
4
|
+
data.tar.gz: f4abea1b5f90fef451168e652ced7a42862d3b21fbb38d5b6b3945f6344fcd57
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 558a813bcbd87351a1826bb0dcd0dd70367d205c8dd7c85238fb95a0499e4e17fc94bfdbdc2fadf4595be0c907c6f4d11b659c38da34580e827cb12cca738898
|
7
|
+
data.tar.gz: 5002ed7ed240491bf64b4313dc75a87537b0f5da8bd8a307ab738394eeb773d7f6e8d344dd7953d43ad1c2e810498a65021edf59944a7a1b4c1ea590e849c56b
|
data/lib/filecache.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'digest/md5'
|
2
2
|
require 'fileutils'
|
3
3
|
|
4
|
-
# A file-based caching library. It uses Marshal::dump and Marshal::load
|
4
|
+
# A file-based caching library. It uses Marshal::dump and Marshal::load
|
5
5
|
# to serialize/deserialize cache values - so you should be OK to cache
|
6
6
|
# object values.
|
7
7
|
class FileCache
|
8
|
-
|
8
|
+
|
9
9
|
MAX_DEPTH = 32
|
10
|
-
|
10
|
+
|
11
11
|
# Create a new reference to a file cache system.
|
12
12
|
# domain:: A string that uniquely identifies this caching
|
13
13
|
# system on the given host
|
@@ -25,7 +25,7 @@ class FileCache
|
|
25
25
|
@depth = depth > MAX_DEPTH ? MAX_DEPTH : depth
|
26
26
|
FileUtils.mkdir_p(get_root)
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
# Set a cache value for the given key. If the cache contains an existing value for
|
30
30
|
# the key it will be overwritten.
|
31
31
|
def set(key, value)
|
@@ -33,18 +33,18 @@ class FileCache
|
|
33
33
|
Marshal.dump(value, f)
|
34
34
|
f.close
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
# Return the value for the specified key from the cache. Returns nil if
|
38
38
|
# the value isn't found.
|
39
39
|
def get(key)
|
40
40
|
path = get_path(key)
|
41
41
|
|
42
42
|
# expire
|
43
|
-
if @expiry > 0 && File.
|
43
|
+
if @expiry > 0 && File.exist?(path) && Time.new - File.new(path).mtime >= @expiry
|
44
44
|
FileUtils.rm(path)
|
45
45
|
end
|
46
|
-
|
47
|
-
if File.
|
46
|
+
|
47
|
+
if File.exist?(path)
|
48
48
|
f = File.open(path, "r")
|
49
49
|
result = Marshal.load(f)
|
50
50
|
f.close
|
@@ -53,7 +53,7 @@ class FileCache
|
|
53
53
|
return nil
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# Return the value for the specified key from the cache if the key exists in the
|
58
58
|
# cache, otherwise set the value returned by the block. Returns the value if found
|
59
59
|
# or the value from calling the block that was set.
|
@@ -72,12 +72,12 @@ class FileCache
|
|
72
72
|
|
73
73
|
# Delete ALL data from the cache, regardless of expiry time
|
74
74
|
def clear
|
75
|
-
if File.
|
75
|
+
if File.exist?(get_root)
|
76
76
|
FileUtils.rm_r(get_root)
|
77
77
|
FileUtils.mkdir_p(get_root)
|
78
78
|
end
|
79
79
|
end
|
80
|
-
|
80
|
+
|
81
81
|
# Delete all expired data from the cache
|
82
82
|
def purge
|
83
83
|
@t_purge = Time.new
|
@@ -85,27 +85,27 @@ class FileCache
|
|
85
85
|
end
|
86
86
|
|
87
87
|
#-------- private methods ---------------------------------
|
88
|
-
private
|
88
|
+
private
|
89
89
|
def get_path(key)
|
90
90
|
md5 = Digest::MD5.hexdigest(key.to_s).to_s
|
91
|
-
|
91
|
+
|
92
92
|
dir = File.join(get_root, md5.split(//)[0..@depth - 1])
|
93
93
|
FileUtils.mkdir_p(dir)
|
94
94
|
return File.join(dir, md5)
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
def get_root
|
98
98
|
if @root == nil
|
99
99
|
@root = File.join(@root_dir, @domain)
|
100
100
|
end
|
101
101
|
return @root
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
def purge_dir(dir)
|
105
105
|
Dir.foreach(dir) do |f|
|
106
106
|
next if f =~ /^\.\.?$/
|
107
107
|
path = File.join(dir, f)
|
108
|
-
if File.directory?(path)
|
108
|
+
if File.directory?(path)
|
109
109
|
purge_dir(path)
|
110
110
|
elsif @t_purge - File.new(path).mtime >= @expiry
|
111
111
|
# Ignore files starting with . - we didn't create those
|
@@ -113,7 +113,7 @@ private
|
|
113
113
|
FileUtils.rm(path)
|
114
114
|
end
|
115
115
|
end
|
116
|
-
|
116
|
+
|
117
117
|
# Delete empty directories
|
118
118
|
if Dir.entries(dir).delete_if{|e| e =~ /^\.\.?$/}.empty?
|
119
119
|
Dir.delete(dir)
|
@@ -1,11 +1,7 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
$:.push(File.join(File.dirname(__FILE__), '..', 'lib'))
|
4
|
-
|
5
1
|
require 'filecache'
|
6
2
|
require 'test/unit'
|
7
3
|
|
8
|
-
class
|
4
|
+
class FileCacheTest < Test::Unit::TestCase
|
9
5
|
KEY1 = "key1"
|
10
6
|
VALUE1 = "value1"
|
11
7
|
VALUE2 = "value2"
|
@@ -16,6 +12,25 @@ class TestFileCache < Test::Unit::TestCase
|
|
16
12
|
f.set(KEY1, VALUE1)
|
17
13
|
assert_equal(f.get(KEY1), VALUE1, "set/get")
|
18
14
|
|
15
|
+
f.delete(KEY1)
|
16
|
+
assert_nil(f.get(KEY1), "delete")
|
17
|
+
|
18
|
+
assert_equal(
|
19
|
+
f.get_or_set(KEY1) do
|
20
|
+
VALUE1
|
21
|
+
end,
|
22
|
+
VALUE1,
|
23
|
+
"set_or_get"
|
24
|
+
)
|
25
|
+
|
26
|
+
assert_equal(
|
27
|
+
f.get_or_set(KEY1) do
|
28
|
+
VALUE2
|
29
|
+
end,
|
30
|
+
VALUE1,
|
31
|
+
"set_or_get"
|
32
|
+
)
|
33
|
+
|
19
34
|
f.delete(KEY1)
|
20
35
|
assert_nil(f.get(KEY1), "delete")
|
21
36
|
|
metadata
CHANGED
@@ -1,46 +1,106 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filecache
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Whitaker
|
8
|
-
|
8
|
+
- Leigh McCulloch
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
|
12
|
+
date: 2023-01-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '11'
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '11'
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - "~>"
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '11'
|
31
|
+
- - ">"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rdoc
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4'
|
41
|
+
- - ">"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - "~>"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '4'
|
51
|
+
- - ">"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '4'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: test-unit
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3'
|
61
|
+
- - ">"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '3'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '3'
|
71
|
+
- - ">"
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '3'
|
74
|
+
description:
|
75
|
+
email:
|
15
76
|
executables: []
|
16
77
|
extensions: []
|
17
78
|
extra_rdoc_files: []
|
18
79
|
files:
|
19
80
|
- lib/filecache.rb
|
20
|
-
- tests/
|
21
|
-
homepage:
|
81
|
+
- tests/filecache_test.rb
|
82
|
+
homepage: https://github.com/leighmcculloch/filecache-ruby
|
22
83
|
licenses:
|
23
84
|
- MIT
|
24
85
|
metadata: {}
|
25
|
-
post_install_message:
|
86
|
+
post_install_message:
|
26
87
|
rdoc_options: []
|
27
88
|
require_paths:
|
28
89
|
- lib
|
29
90
|
required_ruby_version: !ruby/object:Gem::Requirement
|
30
91
|
requirements:
|
31
|
-
- -
|
92
|
+
- - ">="
|
32
93
|
- !ruby/object:Gem::Version
|
33
94
|
version: '0'
|
34
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
96
|
requirements:
|
36
|
-
- -
|
97
|
+
- - ">="
|
37
98
|
- !ruby/object:Gem::Version
|
38
99
|
version: '0'
|
39
100
|
requirements: []
|
40
|
-
|
41
|
-
|
42
|
-
signing_key:
|
101
|
+
rubygems_version: 3.4.1
|
102
|
+
signing_key:
|
43
103
|
specification_version: 4
|
44
104
|
summary: A file-based caching library
|
45
105
|
test_files:
|
46
|
-
- tests/
|
106
|
+
- tests/filecache_test.rb
|