cachetastic 1.3.1 → 1.4.0

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.
@@ -46,6 +46,17 @@ class Cachetastic::Adapters::Base
46
46
  end
47
47
  end
48
48
 
49
+ def stats
50
+ cache_name = self.name.to_s.camelize
51
+ adapter_type = self.class.to_s.gsub('Cachetastic::Adapters::', '')
52
+ s = "Cache: #{cache_name}\nStore Type: #{adapter_type}\n"
53
+ if self.servers
54
+ servers = self.servers.join(',')
55
+ s += "Servers: #{servers}"
56
+ end
57
+ puts s
58
+ end
59
+
49
60
  class << self
50
61
  # Merges options for the store in the configuration file with the cachetastic_default_options
51
62
  # found in the configuration file, and returns the results.
@@ -39,4 +39,13 @@ class Cachetastic::Adapters::Drb < Cachetastic::Adapters::Base
39
39
  self.drb_store.delete(self.name, key)
40
40
  end
41
41
 
42
+ def stats
43
+ super
44
+ begin
45
+ self.drb_store.stats if self.drb_store.respond_to? 'stats'
46
+ rescue Exception => e
47
+ puts "Calling stats on the DRb store raised this exception: #{e.message}"
48
+ end
49
+ end
50
+
42
51
  end
@@ -11,25 +11,7 @@
11
11
  # logger_1:
12
12
  # type: file
13
13
  # file: log/file_store_cache.log
14
- class Cachetastic::Adapters::File < Cachetastic::Adapters::Base
15
-
16
- attr_reader :directory
17
- attr_reader :hashed_keys
18
-
19
- def valid?
20
- File.exists?(self.directory)
21
- end
22
-
23
- def setup
24
- @directory = File.join(self.store_options["dir"], self.name.to_s)
25
- FileUtils.mkdir_p(self.directory, :verbose => self.debug?)
26
- @hashed_keys = {}
27
- end
28
-
29
- def expire_all
30
- FileUtils.rm_rf(self.directory, :verbose => self.debug?)
31
- setup
32
- end
14
+ class Cachetastic::Adapters::File < Cachetastic::Adapters::FileBase
33
15
 
34
16
  def get(key)
35
17
  full_path = full_path_from_dir(get_key_directoy(key, false))
@@ -56,37 +38,12 @@ class Cachetastic::Adapters::File < Cachetastic::Adapters::Base
56
38
  end
57
39
  end
58
40
 
59
- def delete(key, delay = 0)
60
- if delay <= 0
61
- FileUtils.rm_rf(get_key_directoy(key), :verbose => self.debug?)
62
- else
63
- so = self.get(key)
64
- if so
65
- self.set(so.key, so.value, delay)
66
- end
67
- end
41
+ protected
42
+ def store_file_name
43
+ return STORE_FILE_NAME
68
44
  end
69
45
 
70
46
  private
71
- def get_key_directoy(key, mkdir = true)
72
- hkey = self.hashed_keys[key.to_sym]
73
- if hkey.nil?
74
- path = File.join(self.directory, Cachetastic::Helpers::FileUtils.directory_from_key(key))
75
- self.hashed_keys[key.to_sym] = path
76
- hkey = path
77
- end
78
- FileUtils.mkdir_p(hkey, :verbose => self.debug?) if mkdir
79
- hkey
80
- end
81
-
82
- def full_path_from_key(key)
83
- full_path_from_dir(get_key_directoy(key))
84
- end
85
-
86
- def full_path_from_dir(dir)
87
- File.join(dir, STORE_FILE_NAME)
88
- end
89
-
90
47
  STORE_FILE_NAME = "cache.yml"
91
48
 
92
49
  end
@@ -0,0 +1,86 @@
1
+ require 'digest/md5'
2
+ require 'base64'
3
+ class Cachetastic::Adapters::FileBase < Cachetastic::Adapters::Base
4
+
5
+ attr_reader :directory
6
+ attr_reader :hashed_keys
7
+
8
+ def setup
9
+ @directory = File.join(self.store_options["dir"], self.name.to_s)
10
+ FileUtils.mkdir_p(self.directory, :verbose => self.debug?)
11
+ @hashed_keys = {}
12
+ end
13
+
14
+ def valid?
15
+ File.exists?(self.directory)
16
+ end
17
+
18
+ def stats
19
+ super
20
+ num_files = num_directories = file_size = 0
21
+ everything = Dir.glob("#{self.directory}/**/*")
22
+ everything.reject{|x| x =~ /^\./}.each do |entry|
23
+ if ::File.directory?(entry)
24
+ num_directories += 1
25
+ else
26
+ file_size += ::File.size(entry)
27
+ num_files += 1
28
+ end
29
+ end
30
+ puts "Number of Files: #{num_files}\nNumber of Directories: #{num_directories}\nTotal Size on Disk: #{file_size/1024.to_f} KB\n\n"
31
+ end
32
+
33
+ def delete(key, delay = 0)
34
+ if delay <= 0
35
+ FileUtils.rm_rf(get_key_directoy(key), :verbose => self.debug?)
36
+ else
37
+ so = self.get(key)
38
+ if so
39
+ self.set(so.key, so.value, delay)
40
+ end
41
+ end
42
+ end
43
+
44
+ def expire_all
45
+ FileUtils.rm_rf(self.directory, :verbose => self.debug?)
46
+ setup
47
+ end
48
+
49
+ protected
50
+ def store_file_name
51
+ return "cachetastic.data"
52
+ end
53
+
54
+ private
55
+ def directory_from_key(key)
56
+ hkey = Base64.encode64(Digest::MD5.digest(key))
57
+ hkey.gsub!("==\n", "")
58
+ i = 0
59
+ path = ""
60
+ until i >= hkey.length do
61
+ path = File.join(path, hkey[i..i+2])
62
+ i += 3
63
+ end
64
+ path
65
+ end
66
+
67
+ def full_path_from_key(key)
68
+ full_path_from_dir(get_key_directoy(key))
69
+ end
70
+
71
+ def full_path_from_dir(dir)
72
+ File.join(dir, store_file_name)
73
+ end
74
+
75
+ def get_key_directoy(key, mkdir = true)
76
+ hkey = self.hashed_keys[key.to_sym]
77
+ if hkey.nil?
78
+ path = File.join(self.directory, directory_from_key(key))
79
+ self.hashed_keys[key.to_sym] = path
80
+ hkey = path
81
+ end
82
+ FileUtils.mkdir_p(hkey, :verbose => self.debug?) if mkdir
83
+ hkey
84
+ end
85
+
86
+ end
@@ -18,25 +18,7 @@
18
18
  # type: file
19
19
  # file: log/file_store_cache.log
20
20
  require 'time'
21
- class Cachetastic::Adapters::HtmlFile < Cachetastic::Adapters::Base
22
-
23
- attr_reader :directory
24
- attr_reader :hashed_keys
25
-
26
- def valid?
27
- File.exists?(self.directory)
28
- end
29
-
30
- def setup
31
- @directory = File.join(self.store_options["dir"], self.name.to_s)
32
- FileUtils.mkdir_p(self.directory, :verbose => self.debug?)
33
- @hashed_keys = {}
34
- end
35
-
36
- def expire_all
37
- FileUtils.rm_rf(self.directory, :verbose => self.debug?)
38
- setup
39
- end
21
+ class Cachetastic::Adapters::HtmlFile < Cachetastic::Adapters::FileBase
40
22
 
41
23
  def get(key)
42
24
  full_path = full_path_from_dir(get_key_directoy(key, false))
@@ -59,37 +41,12 @@ class Cachetastic::Adapters::HtmlFile < Cachetastic::Adapters::Base
59
41
  end
60
42
  end
61
43
 
62
- def delete(key, delay = 0)
63
- if delay <= 0
64
- FileUtils.rm_rf(get_key_directoy(key), :verbose => self.debug?)
65
- else
66
- so = self.get(key)
67
- if so
68
- self.set(so.key, so.value, delay)
69
- end
70
- end
44
+ protected
45
+ def store_file_name
46
+ return STORE_FILE_NAME
71
47
  end
72
48
 
73
49
  private
74
- def get_key_directoy(key, mkdir = true)
75
- hkey = self.hashed_keys[key.to_sym]
76
- if hkey.nil?
77
- path = File.join(self.directory, Cachetastic::Helpers::FileUtils.directory_from_key(key))
78
- self.hashed_keys[key.to_sym] = path
79
- hkey = path
80
- end
81
- FileUtils.mkdir_p(hkey, :verbose => self.debug?) if mkdir
82
- hkey
83
- end
84
-
85
- def full_path_from_key(key)
86
- full_path_from_dir(get_key_directoy(key))
87
- end
88
-
89
- def full_path_from_dir(dir)
90
- File.join(dir, STORE_FILE_NAME)
91
- end
92
-
93
50
  def html_to_store_object(html)
94
51
  key = html.match(/<!-- cachetastic: key: (.*) -->/).captures.first
95
52
  expires_at = html.match(/<!-- cachetastic: expires_at: (.*) -->/).captures.first
@@ -44,6 +44,25 @@ class Cachetastic::Adapters::LocalMemory < Cachetastic::Adapters::Base
44
44
  end
45
45
  end
46
46
  end
47
+
48
+ def stats
49
+ super
50
+ num_keys = self.local_store.size
51
+ s = "Number of Entries: #{num_keys}\n"
52
+ if num_keys > 0
53
+ expiries = []
54
+ keys = []
55
+ self.local_store.each do |key,value|
56
+ keys << key
57
+ expiries << value.expires_at
58
+ end
59
+ expiries.sort! {|x, y| x <=> y}
60
+ oldest_expiry = expiries.first
61
+ newest_expiry = expiries.last
62
+ s += "Oldest Entry: #{oldest_expiry}\nNewest Entry: #{newest_expiry}\nKeys: #{keys.inspect}\n"
63
+ end
64
+ puts s + "\n"
65
+ end
47
66
 
48
67
 
49
68
  end
@@ -55,6 +55,32 @@ class Cachetastic::Adapters::Memcache < Cachetastic::Adapters::Base
55
55
  end
56
56
  end
57
57
 
58
+ def stats
59
+ super
60
+ begin
61
+ puts "Memcache stats for all caches:"
62
+ memc = self.conn
63
+ puts Kernel.pp_to_s(memc.stats)
64
+ paths = `sh -c 'echo $PATH'`
65
+ paths = paths.split(':')
66
+ memcached_tool_found = false
67
+ paths.each do |path|
68
+ cmd_path = File.expand_path(File.join(path,'memcached_tool'))
69
+ if File.exists?(cmd_path)
70
+ memcached_tool_found = true
71
+ break
72
+ end
73
+ end
74
+ if memcached_tool_found
75
+ app_config.memcache_servers.each do |server|
76
+ puts `memcached_tool #{server}`
77
+ end
78
+ end
79
+ rescue
80
+ end
81
+ puts ""
82
+ end
83
+
58
84
  protected
59
85
  attr_accessor :conn
60
86
  attr_accessor :version
data/lib/cachetastic.rb CHANGED
@@ -30,11 +30,11 @@ require 'caches/cachetastic_caches_page_cache'
30
30
  require 'caches/cachetastic_caches_rails_session_cache'
31
31
  require 'caches/cachetastic_caches_mack_session_cache'
32
32
  require 'errors/cachetastic_errors_unsupported_adapter'
33
- require 'helpers/cachetastic_helpers_file_utils'
34
33
  require 'adapters/cachetastic_adapters_base'
35
34
  require 'adapters/cachetastic_adapters_store_object'
36
35
  require 'adapters/cachetastic_adapters_memcache'
37
36
  require 'adapters/cachetastic_adapters_local_memory'
37
+ require 'adapters/cachetastic_adapters_file_base'
38
38
  require 'adapters/cachetastic_adapters_file'
39
39
  require 'adapters/cachetastic_adapters_html_file'
40
40
  require 'adapters/cachetastic_adapters_drb'
@@ -2,4 +2,4 @@
2
2
  gem_name: cachetastic
3
3
  package: cachetastic
4
4
  project: magrathea
5
- version: 1.3.1
5
+ version: 1.4.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cachetastic
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - markbates
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2008-02-29 00:00:00 -05:00
13
+ date: 2008-03-12 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -27,6 +27,7 @@ files:
27
27
  - lib/adapters/cachetastic_adapters_base.rb
28
28
  - lib/adapters/cachetastic_adapters_drb.rb
29
29
  - lib/adapters/cachetastic_adapters_file.rb
30
+ - lib/adapters/cachetastic_adapters_file_base.rb
30
31
  - lib/adapters/cachetastic_adapters_html_file.rb
31
32
  - lib/adapters/cachetastic_adapters_local_memory.rb
32
33
  - lib/adapters/cachetastic_adapters_memcache.rb
@@ -40,7 +41,6 @@ files:
40
41
  - lib/cachetastic_logger.rb
41
42
  - lib/errors/cachetastic_errors_unsupported_adapter.rb
42
43
  - lib/helpers/cachetastic_helpers_active_record.rb
43
- - lib/helpers/cachetastic_helpers_file_utils.rb
44
44
  - lib/rails_extensions/cachetastic_active_record_base.rb
45
45
  - lib/rails_extensions/cgi_session_cachetastic_store.rb
46
46
  - lib/tasks/rubyforge_config.yml
@@ -1,22 +0,0 @@
1
- require 'digest/md5'
2
- require 'base64'
3
-
4
- module Cachetastic
5
- module Helpers
6
- module FileUtils
7
-
8
- def self.directory_from_key(key)
9
- hkey = Base64.encode64(Digest::MD5.digest(key))
10
- hkey.gsub!("==\n", "")
11
- i = 0
12
- path = ""
13
- until i >= hkey.length do
14
- path = File.join(path, hkey[i..i+2])
15
- i += 3
16
- end
17
- path
18
- end
19
-
20
- end # FileUtils
21
- end # Helpers
22
- end # Cachetastic