elephas 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.travis.yml +8 -0
- data/.yardopts +1 -0
- data/Gemfile +9 -0
- data/README.md +28 -0
- data/Rakefile +18 -0
- data/doc/Elephas/Cache.html +815 -0
- data/doc/Elephas/Entry.html +1085 -0
- data/doc/Elephas/Exceptions.html +115 -0
- data/doc/Elephas/Providers/Base.html +701 -0
- data/doc/Elephas/Providers/Hash.html +775 -0
- data/doc/Elephas/Providers/RubyOnRails.html +609 -0
- data/doc/Elephas/Providers.html +129 -0
- data/doc/Elephas/Version.html +189 -0
- data/doc/Elephas.html +130 -0
- data/doc/_index.html +207 -0
- data/doc/class_list.html +53 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +328 -0
- data/doc/file.README.html +103 -0
- data/doc/file_list.html +55 -0
- data/doc/frames.html +28 -0
- data/doc/index.html +103 -0
- data/doc/js/app.js +214 -0
- data/doc/js/full_list.js +173 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +300 -0
- data/doc/top-level-namespace.html +112 -0
- data/elephas.gemspec +34 -0
- data/lib/elephas/cache.rb +89 -0
- data/lib/elephas/entry.rb +86 -0
- data/lib/elephas/provider.rb +57 -0
- data/lib/elephas/providers/hash.rb +65 -0
- data/lib/elephas/providers/ruby_on_rails.rb +57 -0
- data/lib/elephas/version.rb +25 -0
- data/lib/elephas.rb +20 -0
- data/spec/coverage_helper.rb +19 -0
- data/spec/elephas/cache_spec.rb +41 -0
- data/spec/elephas/entry_spec.rb +11 -0
- data/spec/elephas/provider_spec.rb +46 -0
- data/spec/elephas/providers/hash_spec.rb +80 -0
- data/spec/elephas/providers/ruby_on_rails_spec.rb +86 -0
- data/spec/spec_helper.rb +16 -0
- metadata +231 -0
@@ -0,0 +1,112 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
6
|
+
<title>
|
7
|
+
Top Level Namespace
|
8
|
+
|
9
|
+
— Documentation by YARD 0.8.2.1
|
10
|
+
|
11
|
+
</title>
|
12
|
+
|
13
|
+
<link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" href="css/common.css" type="text/css" media="screen" charset="utf-8" />
|
16
|
+
|
17
|
+
<script type="text/javascript" charset="utf-8">
|
18
|
+
hasFrames = window.top.frames.main ? true : false;
|
19
|
+
relpath = '';
|
20
|
+
framesUrl = "frames.html#!" + escape(window.location.href);
|
21
|
+
</script>
|
22
|
+
|
23
|
+
|
24
|
+
<script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
|
25
|
+
|
26
|
+
<script type="text/javascript" charset="utf-8" src="js/app.js"></script>
|
27
|
+
|
28
|
+
|
29
|
+
</head>
|
30
|
+
<body>
|
31
|
+
<div id="header">
|
32
|
+
<div id="menu">
|
33
|
+
|
34
|
+
<a href="_index.html">Index</a> »
|
35
|
+
|
36
|
+
|
37
|
+
<span class="title">Top Level Namespace</span>
|
38
|
+
|
39
|
+
|
40
|
+
<div class="noframes"><span class="title">(</span><a href="." target="_top">no frames</a><span class="title">)</span></div>
|
41
|
+
</div>
|
42
|
+
|
43
|
+
<div id="search">
|
44
|
+
|
45
|
+
<a class="full_list_link" id="class_list_link"
|
46
|
+
href="class_list.html">
|
47
|
+
Class List
|
48
|
+
</a>
|
49
|
+
|
50
|
+
<a class="full_list_link" id="method_list_link"
|
51
|
+
href="method_list.html">
|
52
|
+
Method List
|
53
|
+
</a>
|
54
|
+
|
55
|
+
<a class="full_list_link" id="file_list_link"
|
56
|
+
href="file_list.html">
|
57
|
+
File List
|
58
|
+
</a>
|
59
|
+
|
60
|
+
</div>
|
61
|
+
<div class="clear"></div>
|
62
|
+
</div>
|
63
|
+
|
64
|
+
<iframe id="search_frame"></iframe>
|
65
|
+
|
66
|
+
<div id="content"><h1>Top Level Namespace
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
</h1>
|
71
|
+
|
72
|
+
<dl class="box">
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
</dl>
|
82
|
+
<div class="clear"></div>
|
83
|
+
|
84
|
+
<h2>Defined Under Namespace</h2>
|
85
|
+
<p class="children">
|
86
|
+
|
87
|
+
|
88
|
+
<strong class="modules">Modules:</strong> <span class='object_link'><a href="Elephas.html" title="Elephas (module)">Elephas</a></span>
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
</p>
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
</div>
|
104
|
+
|
105
|
+
<div id="footer">
|
106
|
+
Generated on Sat Jul 28 14:29:51 2012 by
|
107
|
+
<a href="http://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
|
108
|
+
0.8.2.1 (ruby-1.9.2).
|
109
|
+
</div>
|
110
|
+
|
111
|
+
</body>
|
112
|
+
</html>
|
data/elephas.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require File.expand_path('../lib/elephas/version', __FILE__)
|
8
|
+
|
9
|
+
Gem::Specification.new do |gem|
|
10
|
+
gem.name = "elephas"
|
11
|
+
gem.version = Elephas::Version::STRING
|
12
|
+
gem.homepage = "http://github.com/ShogunPanda/elephas"
|
13
|
+
gem.summary = %q{A storage agnostic caching framework.}
|
14
|
+
gem.description = %q{A storage agnostic caching framework.}
|
15
|
+
gem.rubyforge_project = "elephas"
|
16
|
+
|
17
|
+
gem.authors = ["Shogun"]
|
18
|
+
gem.email = ["shogun_panda@me.com"]
|
19
|
+
|
20
|
+
gem.files = `git ls-files`.split($\)
|
21
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
22
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
23
|
+
gem.require_paths = ["lib"]
|
24
|
+
|
25
|
+
gem.add_dependency("cowtech-extensions", "~> 2.7.0")
|
26
|
+
|
27
|
+
gem.add_development_dependency("rspec", "~> 2.11.0")
|
28
|
+
gem.add_development_dependency("rake", "~> 0.9.0")
|
29
|
+
gem.add_development_dependency("simplecov", "~> 0.6.0")
|
30
|
+
gem.add_development_dependency("pry", "~> 0.9.10")
|
31
|
+
gem.add_development_dependency("yard", "~> 0.8.0")
|
32
|
+
gem.add_development_dependency("redcarpet", "~> 2.1.0")
|
33
|
+
gem.add_development_dependency("github-markup", "~> 0.7.0")
|
34
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Elephas
|
8
|
+
# This is the main class of the framework. Use only this class to access the cache.
|
9
|
+
class Cache
|
10
|
+
class << self
|
11
|
+
# The provider used for the caching.
|
12
|
+
attr_accessor :provider
|
13
|
+
|
14
|
+
# This is the main method of the framework.
|
15
|
+
# It tries reading a key from the cache. If it doesn't find it, it uses the provided block to compute its value and then store it into the cache for later usages.
|
16
|
+
#
|
17
|
+
# @param key [String] The key to lookup.
|
18
|
+
# @return [Object|Entry] The found or newly-set value associated to the key.
|
19
|
+
def use(key, options = {})
|
20
|
+
rv = nil
|
21
|
+
|
22
|
+
# Get options
|
23
|
+
options = {} if !options.is_a?(::Hash)
|
24
|
+
options = {:ttl => 1.hour, :force => false, :as_entry => false}.merge(options)
|
25
|
+
options[:ttl] == [options[:ttl].to_integer, 0].max
|
26
|
+
options[:force] = options[:force].to_boolean
|
27
|
+
options[:prefix] = options[:prefix].present? ? options[:prefix] : "elephas-#{::Elephas::Version::STRING}-cache"
|
28
|
+
|
29
|
+
# Wrap the final key to ensure we don't have colliding namespaces.
|
30
|
+
fkey = "#{options[:prefix]}[#{key}]"
|
31
|
+
|
32
|
+
# Compute the hash key used for referencing this value
|
33
|
+
options[:hash] = options[:hash] || ::Elephas::Entry.hashify_key(fkey.ensure_string)
|
34
|
+
|
35
|
+
# Check if the storage has the value (if we don't have to skip the cache)
|
36
|
+
rv = self.provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
|
37
|
+
|
38
|
+
if rv.nil? && block_given? then # Try to compute the value from the block
|
39
|
+
rv = yield(key, options)
|
40
|
+
|
41
|
+
if rv && options[:ttl] > 0 then # We have a value and we have to store it
|
42
|
+
rv = ::Elephas::Entry.ensure(rv, key, options) # Make sure is an entry
|
43
|
+
Elephas::Cache.write(fkey, value, options)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return value
|
48
|
+
options[:as_entry] ? rv : rv.value.dup
|
49
|
+
end
|
50
|
+
|
51
|
+
# Reads a value from the cache.
|
52
|
+
#
|
53
|
+
# @param key [String] The key to lookup.
|
54
|
+
# @return [Object|NilClass] The read value or `nil`.
|
55
|
+
def read(key)
|
56
|
+
self.provider.read(key)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Writes a value to the cache.
|
60
|
+
#
|
61
|
+
# @param key [String] The key to associate the value with.
|
62
|
+
# @param value [Object] The value to write. Setting a value to `nil` **doesn't** mean *deleting* the value.
|
63
|
+
# @param options [Hash] A list of options for writing.
|
64
|
+
# TODO: Insert options documentation.
|
65
|
+
# @return [Object] The value itself.
|
66
|
+
def write(key, value, options = {})
|
67
|
+
# TODO: Handle options
|
68
|
+
|
69
|
+
self.provider.write(key, value, options)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Deletes a value from the cache.
|
73
|
+
#
|
74
|
+
# @param key [String] The key to delete.
|
75
|
+
# @return [TrueClass|FalseClass] `true` if the key was in the cache, `false` otherwise.
|
76
|
+
def delete(key)
|
77
|
+
self.provider.delete(key)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Checks if a key exists in the cache.
|
81
|
+
#
|
82
|
+
# @param key [String] The key to lookup.
|
83
|
+
# @return [TrueClass|FalseClass] `true` if the key is in the cache, `false` otherwise.
|
84
|
+
def exists?(key)
|
85
|
+
self.provider.exists?(key)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Elephas
|
8
|
+
# This represents a cache entry.
|
9
|
+
class Entry
|
10
|
+
# The key for this entry.
|
11
|
+
attr_accessor :key
|
12
|
+
|
13
|
+
# The hashed (unique) key for this entry.
|
14
|
+
attr_accessor :hash
|
15
|
+
|
16
|
+
# The value contained in this entry.
|
17
|
+
attr_accessor :value
|
18
|
+
|
19
|
+
# The expected TTL of the entry, in milliseconds.
|
20
|
+
attr_accessor :ttl
|
21
|
+
|
22
|
+
# The last update date of the entry, in UNIX timestamp (with milliseconds).
|
23
|
+
attr_accessor :updated_at
|
24
|
+
|
25
|
+
# Creates a new entry.
|
26
|
+
#
|
27
|
+
# @param key [String] The key for this entry.
|
28
|
+
# @param value [Object] The value contained in this entry.
|
29
|
+
# @param hash [String] The hash for this entry. Should be unique. It is automatically created if not provided.
|
30
|
+
# @param ttl [Integer] The time to live (TTL) for this entry. If set to 0 then the entry is not cached.
|
31
|
+
def initialize(key, value, hash = nil, ttl = 3600)
|
32
|
+
hash = self.class.hashify_key(key) if hash.blank?
|
33
|
+
self.key = key
|
34
|
+
self.hash = hash
|
35
|
+
self.value = value
|
36
|
+
self.ttl = ttl
|
37
|
+
self.refresh
|
38
|
+
end
|
39
|
+
|
40
|
+
# Refreshes the entry.
|
41
|
+
#
|
42
|
+
# @param save [Boolean] If to save the refresh value in the cache.
|
43
|
+
def refresh(save = false)
|
44
|
+
self.updated_at = Time.now.to_f
|
45
|
+
|
46
|
+
# TODO: Set this value in the cache
|
47
|
+
end
|
48
|
+
|
49
|
+
# Checks if the entry is still valid.
|
50
|
+
#
|
51
|
+
# @param provider [Provider::Base] The provider to use for the check.
|
52
|
+
# @return [Boolean] `true` if the entry is still valid, `false` otherwise.
|
53
|
+
def valid?(provider = nil)
|
54
|
+
provider ||= ::Elephas::Cache.provider
|
55
|
+
provider.now - self.updated_at < self.ttl
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns a unique hash for the key.
|
59
|
+
#
|
60
|
+
# @param key [String] The key to hashify.
|
61
|
+
# @return [String] An unique hash for the key.
|
62
|
+
def self.hashify_key(key)
|
63
|
+
Digest::SHA2.hexdigest(key.ensure_string)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Ensure that the value is an Entry.
|
67
|
+
#
|
68
|
+
# @param value [Object] The object to check.
|
69
|
+
# @param key [Object] The key associated to this object.
|
70
|
+
# @return [Entry] The wrapped object.
|
71
|
+
def self.ensure(value, key, options = {})
|
72
|
+
rv = value
|
73
|
+
|
74
|
+
if !rv.is_a?(::Elephas::Entry) then
|
75
|
+
options = {} if !options.is_a?(Hash)
|
76
|
+
|
77
|
+
ttl = [options[:ttl].to_integer, 0].max
|
78
|
+
hash = options[:hash] || ::Elephas::Entry.hashify_key(key.ensure_string)
|
79
|
+
|
80
|
+
rv = ::Elephas::Entry.new(key, rv, hash, ttl)
|
81
|
+
end
|
82
|
+
|
83
|
+
rv
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Elephas
|
8
|
+
# This module hosts all the storage system for the caches.
|
9
|
+
module Providers
|
10
|
+
|
11
|
+
# The base provider, with all methods a valid provider should override.
|
12
|
+
module Base
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
# Reads a value from the cache.
|
16
|
+
#
|
17
|
+
# @param key [String] The key to lookup.
|
18
|
+
# @return [Entry|NilClass] The read value or `nil`.
|
19
|
+
def read(key)
|
20
|
+
raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Writes a value to the cache.
|
24
|
+
#
|
25
|
+
# @param key [String] The key to associate the value with.
|
26
|
+
# @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
|
27
|
+
# @param options [Hash] A list of options for writing. @see Elephas::Cache.write
|
28
|
+
# @return [Object] The value itself.
|
29
|
+
def write(key, value, options = {})
|
30
|
+
raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
|
31
|
+
end
|
32
|
+
|
33
|
+
# Deletes a value from the cache.
|
34
|
+
#
|
35
|
+
# @param key [String] The key to delete.
|
36
|
+
# @return [TrueClass|FalseClass] `true` if the key was in the cache, `false` otherwise.
|
37
|
+
def delete(key)
|
38
|
+
raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
|
39
|
+
end
|
40
|
+
|
41
|
+
# Checks if a key exists in the cache.
|
42
|
+
#
|
43
|
+
# @param key [String] The key to lookup.
|
44
|
+
# @return [TrueClass|FalseClass] `true` if the key is in the cache, `false` otherwise.
|
45
|
+
def exists?(key)
|
46
|
+
raise ArgumentError.new("A Elephas::Providers subclass should override this module.")
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns the current time for comparing with entries TTL.
|
50
|
+
#
|
51
|
+
# @return [Object] A representation of the current time.
|
52
|
+
def now
|
53
|
+
::Time.now.to_f
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Elephas
|
8
|
+
module Providers
|
9
|
+
# This is a simple providers, which uses an hash for storing the values.
|
10
|
+
class Hash
|
11
|
+
include Elephas::Providers::Base
|
12
|
+
|
13
|
+
# The internal hash used by the provider.
|
14
|
+
attr_accessor :data
|
15
|
+
|
16
|
+
# Initialize the provider
|
17
|
+
# @param data [Hash] The initial data stored.
|
18
|
+
def initialize(data = nil)
|
19
|
+
data = {} if !data || !data.is_a?(::Hash)
|
20
|
+
@data = data
|
21
|
+
end
|
22
|
+
|
23
|
+
# Reads a value from the cache.
|
24
|
+
#
|
25
|
+
# @param key [String] The key to lookup.
|
26
|
+
# @return [Entry|NilClass] The read value or `nil`.
|
27
|
+
def read(key)
|
28
|
+
self.exists?(key) ? @data[key.ensure_string] : nil
|
29
|
+
end
|
30
|
+
|
31
|
+
# Writes a value to the cache.
|
32
|
+
#
|
33
|
+
# @param key [String] The key to associate the value with.
|
34
|
+
# @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
|
35
|
+
# @param options [Hash] A list of options for writing. @see Elephas::Cache.write
|
36
|
+
# @return [Object] The value itself.
|
37
|
+
def write(key, value, options = {})
|
38
|
+
fvalue = ::Elephas::Entry.ensure(value, key, options)
|
39
|
+
fvalue.refresh
|
40
|
+
@data[key.ensure_string] = fvalue
|
41
|
+
value
|
42
|
+
end
|
43
|
+
|
44
|
+
# Deletes a value from the cache.
|
45
|
+
#
|
46
|
+
# @param key [String] The key to delete.
|
47
|
+
# @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
48
|
+
def delete(key)
|
49
|
+
fkey = key.ensure_string
|
50
|
+
rv = @data.has_key?(fkey)
|
51
|
+
@data.delete(fkey)
|
52
|
+
rv
|
53
|
+
end
|
54
|
+
|
55
|
+
# Checks if a key exists in the cache.
|
56
|
+
#
|
57
|
+
# @param key [String] The key to lookup.
|
58
|
+
# @return [Boolean] `true` if the key is in the cache, `false` otherwise.
|
59
|
+
def exists?(key)
|
60
|
+
fkey = key.ensure_string
|
61
|
+
@data.has_key?(fkey) && @data[fkey].valid?
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
module Elephas
|
8
|
+
module Providers
|
9
|
+
# This is a Ruby on Rails providers, which uses Rails.cache.
|
10
|
+
class RubyOnRails
|
11
|
+
include Elephas::Providers::Base
|
12
|
+
|
13
|
+
# Reads a value from the cache.
|
14
|
+
#
|
15
|
+
# @param key [String] The key to lookup.
|
16
|
+
# @return [Entry|NilClass] The read value or `nil`.
|
17
|
+
def read(key)
|
18
|
+
self.exists?(key) ? Rails.cache.read(key) : nil
|
19
|
+
end
|
20
|
+
|
21
|
+
# Writes a value to the cache.
|
22
|
+
#
|
23
|
+
# @param key [String] The key to associate the value with.
|
24
|
+
# @param value [Object] The value to write. **Setting a value to `nil` doesn't mean *deleting* the value.
|
25
|
+
# @param options [Hash] A list of options for writing. @see Elephas::Cache.write
|
26
|
+
# @return [Object] The value itself.
|
27
|
+
def write(key, value, options = {})
|
28
|
+
ttl = [options[:ttl].to_integer, 0].max
|
29
|
+
fvalue = ::Elephas::Entry.ensure(value, key, options)
|
30
|
+
fvalue.refresh
|
31
|
+
|
32
|
+
Rails.cache.write(key, value, :expires_in => ttl)
|
33
|
+
value
|
34
|
+
end
|
35
|
+
|
36
|
+
# Deletes a value from the cache.
|
37
|
+
#
|
38
|
+
# @param key [String] The key to delete.
|
39
|
+
# @return [Boolean] `true` if the key was in the cache, `false` otherwise.
|
40
|
+
def delete(key)
|
41
|
+
fkey = key.ensure_string
|
42
|
+
rv = Rails.cache.exist?(fkey)
|
43
|
+
Rails.cache.delete(fkey)
|
44
|
+
rv
|
45
|
+
end
|
46
|
+
|
47
|
+
# Checks if a key exists in the cache.
|
48
|
+
#
|
49
|
+
# @param key [String] The key to lookup.
|
50
|
+
# @return [Boolean] `true` if the key is in the cache, `false` otherwise.
|
51
|
+
def exists?(key)
|
52
|
+
fkey = key.ensure_string
|
53
|
+
Rails.cache.exist?(fkey) && Rails.cache.read(fkey).valid?
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
# This is the top level module for elephas gem.
|
8
|
+
module Elephas
|
9
|
+
# The current version of elephas, according to semantic versioning.
|
10
|
+
#
|
11
|
+
# @see http://semver.org
|
12
|
+
module Version
|
13
|
+
# The major version.
|
14
|
+
MAJOR = 0
|
15
|
+
|
16
|
+
# The minor version.
|
17
|
+
MINOR = 1
|
18
|
+
|
19
|
+
# The patch version.
|
20
|
+
PATCH = 0
|
21
|
+
|
22
|
+
# The current version of elephas.
|
23
|
+
STRING = [MAJOR, MINOR, PATCH].compact.join(".")
|
24
|
+
end
|
25
|
+
end
|
data/lib/elephas.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
$KCODE='UTF8' if RUBY_VERSION < '1.9'
|
8
|
+
|
9
|
+
require "cowtech-extensions"
|
10
|
+
require "digest/sha2"
|
11
|
+
Cowtech::Extensions.load!("boolean", "math", "object")
|
12
|
+
|
13
|
+
require "elephas/version" if !defined?(Elephas::Version)
|
14
|
+
require "elephas/entry"
|
15
|
+
require "elephas/provider"
|
16
|
+
require "elephas/providers/hash"
|
17
|
+
require "elephas/providers/ruby_on_rails"
|
18
|
+
require "elephas/cache"
|
19
|
+
|
20
|
+
Elephas::Cache.provider = defined?(Rails) ? Elephas::Providers::RubyOnRails.new : Elephas::Providers::Hash.new
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "simplecov"
|
8
|
+
require "pathname"
|
9
|
+
|
10
|
+
if ENV["ELEPHAS_COVERAGE"] == "TRUE" then
|
11
|
+
root = Pathname.new(File.dirname(__FILE__)) + ".."
|
12
|
+
|
13
|
+
SimpleCov.start do
|
14
|
+
add_filter do |src_file|
|
15
|
+
path = Pathname.new(src_file.filename).relative_path_from(root).to_s
|
16
|
+
path !~ /^lib/
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Elephas::Cache do
|
10
|
+
pending ".use" do
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".read" do
|
15
|
+
it "should be forwarded to the provider" do
|
16
|
+
Elephas::Cache.provider.should_receive(:read)
|
17
|
+
Elephas::Cache.read("KEY")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".write" do
|
22
|
+
it "should be forwarded to the provider" do
|
23
|
+
Elephas::Cache.provider.should_receive(:write)
|
24
|
+
Elephas::Cache.write("KEY", "VALUE")
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".delete" do
|
29
|
+
it "should be forwarded to the provider" do
|
30
|
+
Elephas::Cache.provider.should_receive(:delete)
|
31
|
+
Elephas::Cache.delete("KEY")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".exists?" do
|
36
|
+
it "should be forwarded to the provider" do
|
37
|
+
Elephas::Cache.provider.should_receive(:exists?)
|
38
|
+
Elephas::Cache.exists?("KEY")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Elephas::Entry do
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# This file is part of the elephas gem. Copyright (C) 2012 and above Shogun <shogun_panda@me.com>.
|
4
|
+
# Licensed under the MIT license, which can be found at http://www.opensource.org/licenses/mit-license.php.
|
5
|
+
#
|
6
|
+
|
7
|
+
require "spec_helper"
|
8
|
+
|
9
|
+
describe Elephas::Providers::Base do
|
10
|
+
class BaseProvider
|
11
|
+
include Elephas::Providers::Base
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:provider) { BaseProvider.new }
|
15
|
+
|
16
|
+
describe ".read" do
|
17
|
+
it "should raise an ArgumentError exception" do
|
18
|
+
expect{ provider.read("KEY") }.to raise_error(ArgumentError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe ".write" do
|
23
|
+
it "should raise an ArgumentError exception" do
|
24
|
+
expect{ provider.write("KEY", "VALUE") }.to raise_error(ArgumentError)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe ".delete" do
|
29
|
+
it "should raise an ArgumentError exception" do
|
30
|
+
expect{ provider.delete("KEY") }.to raise_error(ArgumentError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe ".exists?" do
|
35
|
+
it "should raise an ArgumentError exception" do
|
36
|
+
expect{ provider.exists?("KEY") }.to raise_error(ArgumentError)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#now" do
|
41
|
+
it "return a representation for the current time" do
|
42
|
+
Time.stub(:now).and_return(123.456)
|
43
|
+
expect(provider.now).to eq(Time.now.to_f)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|