filecache 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 682c98f59e744d1960f421d0cd6ba803e57beade
4
- data.tar.gz: 68e718d14e8360d92324411f6b4d24850b73719d
2
+ SHA256:
3
+ metadata.gz: 828ce5557d7b60a0deab1418bff56079058fbe6cd2247b50425e0b0b0116b556
4
+ data.tar.gz: f4abea1b5f90fef451168e652ced7a42862d3b21fbb38d5b6b3945f6344fcd57
5
5
  SHA512:
6
- metadata.gz: a4ee039ee1592e9323df57cd14e39b9dfdcfe6cc92060074247bfdd4b7eeed6c18c44d435c67880ac61d54f42a7445d9cf2393cfa60dab387eaab1fda1e7ad87
7
- data.tar.gz: ed527a3c1f5fcee869e837cd6de56e644bd09524307beb870f0ad515fe55c9585d14aff6f16b1c2833d7379b43766d5a0ec59829204ea1a72023384698a20935
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.exists?(path) && Time.new - File.new(path).mtime >= @expiry
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.exists?(path)
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.exists?(get_root)
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)
@@ -12,6 +12,25 @@ class FileCacheTest < Test::Unit::TestCase
12
12
  f.set(KEY1, VALUE1)
13
13
  assert_equal(f.get(KEY1), VALUE1, "set/get")
14
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
+
15
34
  f.delete(KEY1)
16
35
  assert_nil(f.get(KEY1), "delete")
17
36
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filecache
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Whitaker
8
8
  - Leigh McCulloch
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-22 00:00:00.000000000 Z
12
+ date: 2023-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -71,8 +71,8 @@ dependencies:
71
71
  - - ">"
72
72
  - !ruby/object:Gem::Version
73
73
  version: '3'
74
- description:
75
- email:
74
+ description:
75
+ email:
76
76
  executables: []
77
77
  extensions: []
78
78
  extra_rdoc_files: []
@@ -83,7 +83,7 @@ homepage: https://github.com/leighmcculloch/filecache-ruby
83
83
  licenses:
84
84
  - MIT
85
85
  metadata: {}
86
- post_install_message:
86
+ post_install_message:
87
87
  rdoc_options: []
88
88
  require_paths:
89
89
  - lib
@@ -98,9 +98,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.5.1
103
- signing_key:
101
+ rubygems_version: 3.4.1
102
+ signing_key:
104
103
  specification_version: 4
105
104
  summary: A file-based caching library
106
105
  test_files: