diskcached 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/Benchmark.md +0 -2
  2. data/Gemfile +4 -0
  3. data/HISTORY.md +11 -0
  4. data/README.md +1 -15
  5. data/lib/diskcached.rb +48 -33
  6. metadata +4 -6
@@ -12,8 +12,6 @@ it holds up will and even should provide slightly faster reads.
12
12
  **Warning:** Tests do not pass and therefore this isn't expected
13
13
  to actaully work on ruby 1.8.7 at this point. I'm including the
14
14
  benchmarks for it as an academic excercise.
15
-
16
- ## Ruby 1.8.7
17
15
 
18
16
  #### small string * 100000
19
17
 
data/Gemfile CHANGED
@@ -6,8 +6,11 @@ end
6
6
 
7
7
  group :development do
8
8
  gem 'rdoc'
9
+ end
9
10
 
11
+ group :benchmark do
10
12
  # for benchmark only
13
+ # requires sudo apt-get install libsasl2-dev
11
14
  gem 'memcached'
12
15
  end
13
16
 
@@ -16,3 +19,4 @@ group :test do
16
19
  gem 'simplecov', :require => false
17
20
  end
18
21
 
22
+
@@ -0,0 +1,11 @@
1
+ ### 1.1.0
2
+
3
+ * refactored garbage collection
4
+ * fixed bug where auto gc happens every time once initial timeout duration has past
5
+ * allow for turning auto gc on and off
6
+ * cap auto gc timeout at 600 seconds
7
+ * other optimizations
8
+ * removed 'expire!' for 'delete'
9
+ * removed 'expire_all!' for 'flush'
10
+ * removed 'set_or_get' and 'sog' aliases
11
+
data/README.md CHANGED
@@ -1,17 +1,3 @@
1
1
  # Diskcached
2
2
 
3
- #### Test Status [![Build Status](https://secure.travis-ci.org/jmervine/diskcached.png?branch=master)](http://travis-ci.org/jmervine/diskcached)
4
-
5
- #### Installation
6
-
7
- gem install diskcached
8
-
9
- #### [Documentation](http://jmervine.github.com/diskcached/doc/index.html)
10
-
11
- #### [Coverage](http://jmervine.github.com/diskcached/coverage/index.html)
12
-
13
- #### History
14
-
15
- * 1.0.0 / 2012-07-03
16
- * Pre-release version, widely untested outside of the actual tests.
17
-
3
+ See: http://rubyops.net/2012/07/06/diskcached_simple_disk_cacheing_for_ruby
@@ -1,8 +1,7 @@
1
1
  # @author Joshua P. Mervine <joshua@mervine.net>
2
- #
3
2
  class Diskcached
4
3
  # version for gem
5
- VERSION = '1.0.1'
4
+ VERSION = '1.1.0'
6
5
 
7
6
  # disk location for cache store
8
7
  attr_reader :store
@@ -10,47 +9,76 @@ class Diskcached
10
9
  # cache timeout
11
10
  attr_reader :timeout
12
11
 
13
- # time of last #garbage_collect_expired_caches
14
- attr_reader :last_gc
12
+ # time of last #garbage_collect
13
+ attr_reader :gc_last
14
+
15
+ # should auto_garbage_collect
16
+ attr_reader :gc_auto
17
+
18
+ # how often to auto_garbage_collect
19
+ attr_reader :gc_time
15
20
 
16
21
  # initialize object
17
22
  # - set #store to passed or default ('/tmp/cache')
18
23
  # - set #timeout to passed or default ('600')
19
- # - set #last_gc to current time
24
+ # - set #gc_last to current time
20
25
  # - run #ensure_store_directory
21
- def initialize store="/tmp/cache", timeout=600
26
+ def initialize store="/tmp/cache", timeout=600, autogc=true
22
27
  @store = store
23
28
  @timeout = timeout
24
- @last_gc = Time.now
29
+ if timeout.nil?
30
+ @gc_last, @gc_time = nil
31
+ @gc_auto = false
32
+ else
33
+ @gc_last = Time.now
34
+ @gc_auto = autogc
35
+ @gc_time = (timeout < 600 ? timeout : 600)
36
+ end
25
37
  ensure_store_directory
26
38
  end
27
39
 
28
40
  # return true if cache with 'key' is expired
29
41
  def expired? key
42
+ return false if timeout.nil?
30
43
  mtime = read_cache_mtime(key)
31
44
  return (mtime.nil? || mtime+timeout <= Time.now)
32
45
  end
33
46
 
34
47
  # expire cache with 'key'
35
- def expire! key
48
+ def delete key
36
49
  File.delete( cache_file(key) ) if File.exists?( cache_file(key) )
37
50
  end
38
- alias :delete :expire!
39
51
 
40
52
  # expire (delete) all caches in #store directory
41
- def expire_all!
53
+ def flush
42
54
  Dir[ File.join( store, '*.cache' ) ].each do |file|
43
55
  File.delete(file)
44
56
  end
45
57
  end
46
- alias :flush :expire_all!
58
+
59
+ # flush expired caches if garbage collection
60
+ # hasn't been run recently
61
+ def flush_expired
62
+ if gc_last && gc_time && gc_last+gc_time <= Time.now
63
+ flush_expired!
64
+ end
65
+ end
66
+
67
+ # flash expired caches, ingoring when garbage
68
+ # collection was last run
69
+ def flush_expired!
70
+ Dir[ File.join( store, "*.cache" ) ].each do |f|
71
+ if (File.mtime(f)+timeout) <= Time.now
72
+ File.delete(f)
73
+ end
74
+ end
75
+ @gc_last = Time.now
76
+ end
47
77
 
48
78
  # create and read cache with 'key'
49
- # - run #garbage_collect_expired_caches
50
79
  # - creates cache if it doesn't exist
51
80
  # - reads cache if it exists
52
81
  def cache key
53
- garbage_collect_expired_caches
54
82
  begin
55
83
  if expired?(key)
56
84
  content = Proc.new { yield }.call
@@ -62,23 +90,22 @@ class Diskcached
62
90
  return nil
63
91
  end
64
92
  end
65
- alias :set_or_get :cache
66
- alias :sog :cache
67
93
 
68
94
  # set cache with 'key'
69
- # - run #garbage_collect_expired_caches
95
+ # - run #auto_garbage_collect
70
96
  # - creates cache if it doesn't exist
71
97
  def set key, value
72
- garbage_collect_expired_caches
73
98
  begin
74
99
  write_cache_file( key, Marshal::dump(value) )
100
+ flush_expired if gc_auto
75
101
  return true
76
102
  rescue
103
+ flush_expired if gc_auto
77
104
  return false
78
105
  end
79
106
  end
80
- alias :add :set
81
- alias :replace :set
107
+ alias :add :set # for memcached compatability
108
+ alias :replace :set # for memcached compatability
82
109
 
83
110
  # get cache with 'key'
84
111
  # - reads cache if it exists and isn't expired
@@ -87,15 +114,16 @@ class Diskcached
87
114
  # which exist and aren't expired, it raises
88
115
  # Diskcache::NotFound if none are available
89
116
  def get key
90
- garbage_collect_expired_caches
91
117
  begin
92
118
  if key.is_a? Array
93
119
  hash = {}
94
120
  key.each do |k|
95
121
  hash[k] = Marshal::load(read_cache_file(k)) unless expired?(k)
96
122
  end
123
+ flush_expired if gc_auto
97
124
  return hash unless hash.empty?
98
125
  else
126
+ flush_expired if gc_auto
99
127
  return Marshal::load(read_cache_file(key)) unless expired?(key)
100
128
  end
101
129
  raise Diskcached::NotFound
@@ -109,20 +137,7 @@ class Diskcached
109
137
  File.join( store, key+".cache" )
110
138
  end
111
139
 
112
-
113
140
  private
114
- # delete all expired caches every #timeout seconds
115
- # on being called
116
- def garbage_collect_expired_caches
117
- if last_gc+timeout <= Time.now
118
- Dir[ File.join( store, "*.cache" ) ].each do |f|
119
- if (File.mtime(f)+timeout) <= Time.now
120
- File.delete(f)
121
- end
122
- end
123
- end
124
- end
125
-
126
141
  # creates the actual cache file
127
142
  def write_cache_file key, content
128
143
  f = File.open( cache_file(key), "w+" )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diskcached
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-06 00:00:00.000000000 Z
12
+ date: 2012-07-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -69,10 +69,11 @@ extra_rdoc_files: []
69
69
  files:
70
70
  - lib/diskcached.rb
71
71
  - README.md
72
+ - HISTORY.md
72
73
  - Benchmark.md
73
74
  - Gemfile
74
75
  - Rakefile
75
- homepage: http://jmervine.github.com/diskcached/
76
+ homepage: http://diskcached.rubyops.net/
76
77
  licenses: []
77
78
  post_install_message:
78
79
  rdoc_options: []
@@ -84,9 +85,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
84
85
  - - ! '>='
85
86
  - !ruby/object:Gem::Version
86
87
  version: '0'
87
- segments:
88
- - 0
89
- hash: -4576607150070484010
90
88
  required_rubygems_version: !ruby/object:Gem::Requirement
91
89
  none: false
92
90
  requirements: