ramaze 2011.01.30 → 2011.07.25
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.
- data/.gitignore +2 -1
- data/.mailmap +2 -0
- data/.rvmrc +1 -0
- data/README.md +119 -197
- data/Rakefile +14 -97
- data/bin/ramaze +6 -14
- data/doc/AUTHORS +8 -4
- data/doc/CHANGELOG +3784 -3339
- data/examples/app/chat/start.rb +2 -2
- data/lib/proto/app.rb +2 -3
- data/lib/proto/config.ru +4 -5
- data/lib/proto/controller/init.rb +11 -4
- data/lib/proto/controller/main.rb +12 -7
- data/lib/proto/layout/default.xhtml +56 -23
- data/lib/proto/model/init.rb +3 -1
- data/lib/proto/public/css/grid.css +107 -0
- data/lib/proto/public/css/layout.css +81 -0
- data/lib/proto/public/css/reset.css +123 -0
- data/lib/proto/public/css/text.css +109 -0
- data/lib/proto/public/images/bg.png +0 -0
- data/lib/proto/spec/main.rb +2 -2
- data/lib/proto/start.rb +11 -1
- data/lib/proto/view/index.xhtml +27 -23
- data/lib/ramaze.rb +0 -1
- data/lib/ramaze/app.rb +85 -12
- data/lib/ramaze/app_graph.rb +107 -0
- data/lib/ramaze/bin/console.rb +87 -0
- data/lib/ramaze/bin/create.rb +94 -0
- data/lib/ramaze/bin/helper.rb +107 -0
- data/lib/ramaze/bin/restart.rb +95 -0
- data/lib/ramaze/bin/runner.rb +141 -0
- data/lib/ramaze/bin/start.rb +206 -0
- data/lib/ramaze/bin/status.rb +152 -0
- data/lib/ramaze/bin/stop.rb +112 -0
- data/lib/ramaze/cache.rb +9 -4
- data/lib/ramaze/cache/localmemcache.rb +10 -13
- data/lib/ramaze/cache/lru.rb +49 -7
- data/lib/ramaze/cache/memcache.rb +170 -92
- data/lib/ramaze/cache/sequel.rb +301 -118
- data/lib/ramaze/controller.rb +108 -9
- data/lib/ramaze/controller/default.rb +15 -2
- data/lib/ramaze/current.rb +14 -2
- data/lib/ramaze/dependencies.rb +46 -0
- data/lib/ramaze/files.rb +38 -3
- data/lib/ramaze/gestalt.rb +12 -12
- data/lib/ramaze/helper.rb +0 -2
- data/lib/ramaze/helper/auth.rb +30 -23
- data/lib/ramaze/helper/blue_form.rb +175 -126
- data/lib/ramaze/helper/csrf.rb +76 -91
- data/lib/ramaze/helper/email.rb +105 -0
- data/lib/ramaze/helper/erector.rb +16 -15
- data/lib/ramaze/helper/gestalt.rb +2 -2
- data/lib/ramaze/helper/layout.rb +89 -73
- data/lib/ramaze/helper/link.rb +7 -6
- data/lib/ramaze/helper/localize.rb +6 -5
- data/lib/ramaze/helper/markaby.rb +25 -23
- data/lib/ramaze/helper/maruku.rb +3 -3
- data/lib/ramaze/helper/paginate.rb +19 -27
- data/lib/ramaze/helper/remarkably.rb +3 -3
- data/lib/ramaze/helper/request_accessor.rb +3 -3
- data/lib/ramaze/helper/send_file.rb +12 -8
- data/lib/ramaze/helper/simple_captcha.rb +5 -6
- data/lib/ramaze/helper/stack.rb +7 -4
- data/lib/ramaze/helper/tagz.rb +10 -11
- data/lib/ramaze/helper/thread.rb +19 -16
- data/lib/ramaze/helper/ultraviolet.rb +7 -4
- data/lib/ramaze/helper/user.rb +40 -21
- data/lib/ramaze/helper/xhtml.rb +29 -20
- data/lib/ramaze/log.rb +3 -11
- data/lib/ramaze/log/analogger.rb +5 -4
- data/lib/ramaze/log/growl.rb +9 -7
- data/lib/ramaze/log/hub.rb +3 -5
- data/lib/ramaze/log/informer.rb +15 -12
- data/lib/ramaze/log/knotify.rb +3 -5
- data/lib/ramaze/log/logger.rb +3 -5
- data/lib/ramaze/log/logging.rb +6 -8
- data/lib/ramaze/log/rotatinginformer.rb +27 -17
- data/lib/ramaze/log/syslog.rb +7 -7
- data/lib/ramaze/log/xosd.rb +3 -5
- data/lib/ramaze/middleware_compiler.rb +27 -4
- data/lib/ramaze/reloader.rb +50 -12
- data/lib/ramaze/reloader/watch_inotify.rb +4 -5
- data/lib/ramaze/reloader/watch_stat.rb +3 -3
- data/lib/ramaze/request.rb +18 -8
- data/lib/ramaze/response.rb +38 -7
- data/lib/ramaze/rest.rb +36 -0
- data/lib/ramaze/setup.rb +101 -31
- data/lib/ramaze/spec.rb +1 -1
- data/lib/ramaze/spec/bacon.rb +6 -3
- data/lib/ramaze/spec/helper/bacon.rb +0 -1
- data/lib/ramaze/version.rb +1 -1
- data/lib/ramaze/view.rb +2 -11
- data/lib/ramaze/view/erector.rb +46 -31
- data/lib/ramaze/view/erubis.rb +7 -3
- data/lib/ramaze/view/ezamar.rb +7 -3
- data/lib/ramaze/view/gestalt.rb +9 -3
- data/lib/ramaze/view/haml.rb +7 -3
- data/lib/ramaze/view/liquid.rb +3 -3
- data/lib/ramaze/view/lokar.rb +7 -3
- data/lib/ramaze/view/mustache.rb +11 -5
- data/lib/ramaze/view/nagoro.rb +3 -3
- data/lib/ramaze/view/sass.rb +1 -1
- data/lib/ramaze/view/slippers.rb +40 -13
- data/lib/ramaze/view/tagz.rb +9 -5
- data/ramaze.gemspec +23 -128
- data/spec/helper.rb +5 -0
- data/spec/ramaze/bin/app/config.ru +11 -0
- data/spec/ramaze/bin/create.rb +28 -0
- data/spec/ramaze/bin/runner.rb +30 -0
- data/spec/ramaze/bin/start.rb +38 -0
- data/spec/ramaze/cache/memcache.rb +10 -3
- data/spec/ramaze/cache/sequel.rb +7 -0
- data/spec/ramaze/controller/provide_inheritance.rb +0 -10
- data/spec/ramaze/dispatcher/file.rb +19 -15
- data/spec/ramaze/helper/auth.rb +10 -9
- data/spec/ramaze/helper/blue_form.rb +121 -68
- data/spec/ramaze/helper/email.rb +69 -0
- data/spec/ramaze/helper/layout.rb +12 -15
- data/spec/ramaze/helper/layout/alternative.xhtml +5 -0
- data/spec/ramaze/helper/user.rb +2 -0
- data/spec/ramaze/log/growl.rb +14 -1
- data/spec/{contrib → ramaze}/rest.rb +1 -1
- data/spec/ramaze/session/memcache.rb +2 -2
- data/spec/ramaze/view/sass.rb +1 -1
- data/tasks/bacon.rake +3 -3
- data/tasks/gem.rake +17 -18
- data/tasks/rcov.rake +2 -3
- data/tasks/release.rake +8 -65
- data/tasks/setup.rake +10 -8
- data/tasks/todo.rake +9 -5
- data/tasks/yard.rake +3 -2
- metadata +105 -397
- data/MANIFEST +0 -532
- data/TODO.md +0 -19
- data/benchmark/bench_templates/bench.rb +0 -67
- data/benchmark/bench_templates/view/large.erb +0 -79
- data/benchmark/bench_templates/view/large.haml +0 -41
- data/benchmark/bench_templates/view/large.lok +0 -79
- data/benchmark/bench_templates/view/large.xhtml +0 -79
- data/benchmark/bench_templates/view/small.erb +0 -21
- data/benchmark/bench_templates/view/small.haml +0 -12
- data/benchmark/bench_templates/view/small.lok +0 -21
- data/benchmark/bench_templates/view/small.xhtml +0 -21
- data/benchmark/results.txt +0 -131
- data/benchmark/run.rb +0 -355
- data/benchmark/suite/minimal.rb +0 -11
- data/benchmark/suite/no_informer.rb +0 -7
- data/benchmark/suite/no_sessions.rb +0 -9
- data/benchmark/suite/no_template.rb +0 -7
- data/benchmark/suite/simple.rb +0 -5
- data/benchmark/suite/template_erubis.rb +0 -8
- data/benchmark/suite/template_etanni.rb +0 -8
- data/benchmark/suite/template_ezamar.rb +0 -8
- data/benchmark/suite/template_haml.rb +0 -13
- data/benchmark/suite/template_liquid.rb +0 -11
- data/benchmark/suite/template_markaby.rb +0 -9
- data/benchmark/suite/template_nagoro.rb +0 -8
- data/benchmark/suite/template_redcloth.rb +0 -13
- data/benchmark/suite/template_tenjin.rb +0 -8
- data/benchmark/test.rb +0 -35
- data/doc/FAQ +0 -92
- data/doc/INSTALL +0 -92
- data/doc/TODO +0 -29
- data/doc/meta/announcement.txt +0 -119
- data/doc/meta/configuration.txt +0 -163
- data/doc/meta/internals.txt +0 -278
- data/doc/meta/users.kml +0 -64
- data/doc/tutorial/todolist.html +0 -1512
- data/doc/tutorial/todolist.txt +0 -920
- data/examples/app/sourceview/public/coderay.css +0 -104
- data/examples/app/sourceview/public/images/file.gif +0 -0
- data/examples/app/sourceview/public/images/folder.gif +0 -0
- data/examples/app/sourceview/public/images/tv-collapsable-last.gif +0 -0
- data/examples/app/sourceview/public/images/tv-collapsable.gif +0 -0
- data/examples/app/sourceview/public/images/tv-expandable-last.gif +0 -0
- data/examples/app/sourceview/public/images/tv-expandable.gif +0 -0
- data/examples/app/sourceview/public/images/tv-item-last.gif +0 -0
- data/examples/app/sourceview/public/images/tv-item.gif +0 -0
- data/examples/app/sourceview/public/jquery.js +0 -11
- data/examples/app/sourceview/public/jquery.treeview.css +0 -48
- data/examples/app/sourceview/public/jquery.treeview.js +0 -223
- data/examples/app/sourceview/public/sourceview.js +0 -52
- data/examples/app/sourceview/start.rb +0 -79
- data/examples/app/sourceview/view/index.haml +0 -59
- data/examples/helpers/httpdigest.rb +0 -107
- data/lib/proto/public/css/screen.css +0 -30
- data/lib/proto/public/js/jquery.js +0 -7179
- data/lib/ramaze/contrib/addressable_route.rb +0 -56
- data/lib/ramaze/contrib/app_graph.rb +0 -64
- data/lib/ramaze/contrib/email.rb +0 -88
- data/lib/ramaze/contrib/facebook.rb +0 -23
- data/lib/ramaze/contrib/facebook/facebook.rb +0 -171
- data/lib/ramaze/contrib/gettext.rb +0 -113
- data/lib/ramaze/contrib/gettext/mo.rb +0 -155
- data/lib/ramaze/contrib/gettext/parser.rb +0 -46
- data/lib/ramaze/contrib/gettext/po.rb +0 -109
- data/lib/ramaze/contrib/gzip_filter.rb +0 -1
- data/lib/ramaze/contrib/maruku_uv.rb +0 -59
- data/lib/ramaze/contrib/profiling.rb +0 -36
- data/lib/ramaze/contrib/rest.rb +0 -23
- data/lib/ramaze/contrib/sequel/create_join.rb +0 -26
- data/lib/ramaze/contrib/sequel/form_field.rb +0 -129
- data/lib/ramaze/contrib/sequel/image.rb +0 -196
- data/lib/ramaze/contrib/sequel/relation.rb +0 -98
- data/lib/ramaze/helper/httpdigest.rb +0 -96
- data/lib/ramaze/tool/bin.rb +0 -340
- data/lib/ramaze/tool/create.rb +0 -48
- data/lib/ramaze/tool/project_creator.rb +0 -120
- data/lib/ramaze/view/less.rb +0 -12
- data/lib/ramaze/view/maruku.rb +0 -15
- data/lib/ramaze/view/redcloth.rb +0 -21
- data/spec/contrib/addressable_route.rb +0 -30
- data/spec/examples/helpers/httpdigest.rb +0 -64
- data/spec/examples/templates/template_redcloth.rb +0 -13
- data/spec/ramaze/bin/ramaze.rb +0 -96
- data/spec/ramaze/helper/httpdigest.rb +0 -176
- data/spec/ramaze/view/less.rb +0 -60
- data/spec/ramaze/view/less/file.css.less +0 -8
- data/spec/ramaze/view/redcloth.rb +0 -66
- data/spec/ramaze/view/redcloth/external.redcloth +0 -8
- data/tasks/copyright.rake +0 -21
- data/tasks/gem_setup.rake +0 -112
- data/tasks/git.rake +0 -46
- data/tasks/grancher.rake +0 -12
- data/tasks/jquery.rake +0 -15
- data/tasks/manifest.rake +0 -4
- data/tasks/metric_changes.rake +0 -24
- data/tasks/reversion.rake +0 -8
- data/tasks/traits.rake +0 -21
data/lib/ramaze/cache.rb
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
1
|
# Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
|
|
2
2
|
# All files in this distribution are subject to the terms of the Ruby license.
|
|
3
|
-
|
|
4
3
|
require 'innate/cache'
|
|
5
4
|
|
|
6
5
|
module Ramaze
|
|
7
6
|
Cache = Innate::Cache
|
|
8
7
|
|
|
9
|
-
|
|
8
|
+
#:nodoc:
|
|
10
9
|
class Cache
|
|
11
10
|
autoload :LRU, 'ramaze/cache/lru'
|
|
12
11
|
autoload :LocalMemCache, 'ramaze/cache/localmemcache'
|
|
13
12
|
autoload :MemCache, 'ramaze/cache/memcache'
|
|
14
13
|
autoload :Sequel, 'ramaze/cache/sequel'
|
|
15
14
|
|
|
15
|
+
##
|
|
16
|
+
# Clears the cache after a file has been reloaded.
|
|
17
|
+
#
|
|
18
|
+
# @author Michael Fellinger
|
|
19
|
+
# @since 17-07-2009
|
|
20
|
+
#
|
|
16
21
|
def self.clear_after_reload
|
|
17
22
|
action.clear if respond_to?(:action)
|
|
18
23
|
action_value.clear if respond_to?(:action_value)
|
|
19
24
|
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
25
|
+
end # Cache
|
|
26
|
+
end # Ramaze
|
|
@@ -5,7 +5,6 @@ require 'localmemcache'
|
|
|
5
5
|
|
|
6
6
|
module Ramaze
|
|
7
7
|
class Cache
|
|
8
|
-
|
|
9
8
|
# Cache based on the localmemcache library which utilizes mmap to share
|
|
10
9
|
# strings in memory between ruby instances.
|
|
11
10
|
class LocalMemCache
|
|
@@ -19,14 +18,12 @@ module Ramaze
|
|
|
19
18
|
|
|
20
19
|
# Connect to localmemcache
|
|
21
20
|
def cache_setup(host, user, app, name)
|
|
22
|
-
@namespace
|
|
23
|
-
|
|
24
|
-
options = {:namespace => @namespace}.merge(OPTIONS)
|
|
21
|
+
@namespace = [host, user, app, name].compact.join('-')
|
|
22
|
+
options = {:namespace => @namespace}.merge(OPTIONS)
|
|
25
23
|
|
|
26
|
-
@serialize
|
|
24
|
+
@serialize = options.delete(:serialize)
|
|
27
25
|
@serializer = options.delete(:serializer)
|
|
28
|
-
|
|
29
|
-
@store = ::LocalMemCache.new(options)
|
|
26
|
+
@store = ::LocalMemCache.new(options)
|
|
30
27
|
end
|
|
31
28
|
|
|
32
29
|
# Wipe out _all_ data in localmemcached, use with care.
|
|
@@ -35,22 +32,22 @@ module Ramaze
|
|
|
35
32
|
end
|
|
36
33
|
|
|
37
34
|
def cache_delete(*args)
|
|
38
|
-
super{|key| @store.delete(key.to_s); nil }
|
|
35
|
+
super { |key| @store.delete(key.to_s); nil }
|
|
39
36
|
end
|
|
40
37
|
|
|
41
38
|
# NOTE:
|
|
42
39
|
# * We have no way of knowing whether the value really is nil, we
|
|
43
40
|
# assume you wouldn't cache nil and return the default instead.
|
|
44
41
|
def cache_fetch(*args)
|
|
45
|
-
super{|key|
|
|
42
|
+
super { |key|
|
|
46
43
|
value = @store[key.to_s]
|
|
47
44
|
@serializer.load(value) if value
|
|
48
45
|
}
|
|
49
46
|
end
|
|
50
47
|
|
|
51
48
|
def cache_store(*args)
|
|
52
|
-
super{|key, value| @store[key.to_s] = @serializer.dump(value) }
|
|
49
|
+
super { |key, value| @store[key.to_s] = @serializer.dump(value) }
|
|
53
50
|
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
end
|
|
51
|
+
end # LocalMemCache
|
|
52
|
+
end # Cache
|
|
53
|
+
end # Ramaze
|
data/lib/ramaze/cache/lru.rb
CHANGED
|
@@ -3,9 +3,18 @@
|
|
|
3
3
|
|
|
4
4
|
module Ramaze
|
|
5
5
|
class Cache
|
|
6
|
+
##
|
|
7
|
+
# Cache class that uses Ramaze::LRUHash as a storage engine. This cache has
|
|
8
|
+
# the advantage that unlike Innate::Cache::Memory it does not leak memory
|
|
9
|
+
# over time.
|
|
10
|
+
#
|
|
11
|
+
# @author Michael Fellinger
|
|
12
|
+
# @since 17-07-2009
|
|
13
|
+
#
|
|
6
14
|
class LRU
|
|
7
15
|
include Cache::API
|
|
8
16
|
|
|
17
|
+
# Hash containing all the options for the cache.
|
|
9
18
|
OPTIONS = {
|
|
10
19
|
# expiration in seconds
|
|
11
20
|
:expiration => nil,
|
|
@@ -17,26 +26,59 @@ module Ramaze
|
|
|
17
26
|
:max_value => nil,
|
|
18
27
|
}
|
|
19
28
|
|
|
20
|
-
|
|
29
|
+
##
|
|
30
|
+
# Prepares the cache by creating a new instance of Ramaze::LRUHash using
|
|
31
|
+
# the options set in Ramaze::Cache::LRU::OPTIONS.
|
|
32
|
+
#
|
|
33
|
+
# @author Michael Fellinger
|
|
34
|
+
# @since 17-07-2009
|
|
35
|
+
#
|
|
21
36
|
def cache_setup(host, user, app, name)
|
|
22
37
|
@store = Ramaze::LRUHash.new(OPTIONS)
|
|
23
38
|
end
|
|
24
39
|
|
|
40
|
+
##
|
|
41
|
+
# Clears the entire cache.
|
|
42
|
+
#
|
|
43
|
+
# @author Michael Fellinger
|
|
44
|
+
# @since 17-07-2009
|
|
45
|
+
#
|
|
25
46
|
def cache_clear
|
|
26
47
|
@store.clear
|
|
27
48
|
end
|
|
28
49
|
|
|
50
|
+
##
|
|
51
|
+
# Stores a set of data in the cache.
|
|
52
|
+
#
|
|
53
|
+
# @author Michael Fellinger
|
|
54
|
+
# @since 17-07-2009
|
|
55
|
+
# @see Innate::Cache::API#cache_store
|
|
56
|
+
#
|
|
29
57
|
def cache_store(*args)
|
|
30
|
-
super{|key, value| @store[key] = value }
|
|
58
|
+
super { |key, value| @store[key] = value }
|
|
31
59
|
end
|
|
32
60
|
|
|
61
|
+
##
|
|
62
|
+
# Retrieves a set of data from the cache.
|
|
63
|
+
#
|
|
64
|
+
# @author Michael Fellinger
|
|
65
|
+
# @since 17-07-2009
|
|
66
|
+
# @see Innate::Cache::API#cache_fetch
|
|
67
|
+
#
|
|
33
68
|
def cache_fetch(*args)
|
|
34
|
-
super{|key| @store[key] }
|
|
69
|
+
super { |key| @store[key] }
|
|
35
70
|
end
|
|
36
71
|
|
|
72
|
+
##
|
|
73
|
+
# Deletes a set of data from the cache
|
|
74
|
+
#
|
|
75
|
+
# @author Michael Fellinger
|
|
76
|
+
# @since 17-07-2009
|
|
77
|
+
# @see Innate::Cache::API#cache_delete
|
|
78
|
+
#
|
|
37
79
|
def cache_delete(*args)
|
|
38
|
-
super{|key| @store.delete(key) }
|
|
80
|
+
super { |key| @store.delete(key) }
|
|
39
81
|
end
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
82
|
+
end # LRU
|
|
83
|
+
end # Cache
|
|
84
|
+
end # Ramaze
|
|
@@ -1,124 +1,202 @@
|
|
|
1
|
-
|
|
2
|
-
# All files in this distribution are subject to the terms of the Ruby license.
|
|
1
|
+
require 'dalli'
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
# Kgio gives a nice performance boost but it isn't required
|
|
4
|
+
begin; require 'kgio'; rescue LoadError => e; end
|
|
5
5
|
|
|
6
|
+
#:nodoc:
|
|
6
7
|
module Ramaze
|
|
8
|
+
#:nodoc:
|
|
7
9
|
class Cache
|
|
8
|
-
|
|
9
|
-
# Cache
|
|
10
|
-
# store
|
|
10
|
+
##
|
|
11
|
+
# Cache driver for the Memcache storage engine. Memcache is a key/value
|
|
12
|
+
# store that's extremely useful for caching data such as views or API
|
|
13
|
+
# responses. More inforamtion about Memcache can be found on it's website:
|
|
14
|
+
# http://memcached.org/.
|
|
15
|
+
#
|
|
16
|
+
# Note that this cache driver requires the Dalli gem rather than Memcache
|
|
17
|
+
# Client. The reason for this is that the Memcache client hasn't been
|
|
18
|
+
# updated in over a year and Memcache has changed quite a bit. Dalli is also
|
|
19
|
+
# supposed to be faster and better coded. This cache driver will also try to
|
|
20
|
+
# load the kgio Gem if it's installed, if it's not it will just continue to
|
|
21
|
+
# operate but you won't get the nice speed boost.
|
|
22
|
+
#
|
|
23
|
+
# This driver works similar to Ramaze::Cache::Sequel in that it allows you
|
|
24
|
+
# to specify instance specific options uisng the using() method:
|
|
11
25
|
#
|
|
12
|
-
#
|
|
26
|
+
# Ramaze::Cache.options.view = Ramaze::Cache::Memcache.using(:compression => false)
|
|
13
27
|
#
|
|
14
|
-
#
|
|
15
|
-
# a bit of speedup and more functionality
|
|
28
|
+
# All options sent to the using() method will be sent to Dalli.
|
|
16
29
|
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
# without them noticing, but there is also a low limit to the maximum
|
|
20
|
-
# time-to-live. After 30 days, your session will be dropped, no
|
|
21
|
-
# matter what.
|
|
22
|
-
# Please remember that memcache is, first of all, a cache, not a
|
|
23
|
-
# persistence mechanism.
|
|
30
|
+
# @author Yorick Peterse
|
|
31
|
+
# @since 04-05-2011
|
|
24
32
|
#
|
|
25
|
-
# NOTE: If you try to set a higher ttl than allowed, your stored key/value
|
|
26
|
-
# will be expired immediately.
|
|
27
33
|
class MemCache
|
|
34
|
+
include Cache::API
|
|
35
|
+
include Innate::Traited
|
|
36
|
+
|
|
37
|
+
# The maximum Time To Live that can be used in Memcache
|
|
28
38
|
MAX_TTL = 2592000
|
|
29
39
|
|
|
30
|
-
|
|
40
|
+
# Hash containing the default configuration options to use for Dalli
|
|
41
|
+
trait :default => {
|
|
42
|
+
# The default TTL for each item
|
|
43
|
+
:expires_in => 604800,
|
|
44
|
+
|
|
45
|
+
# Compresses everything with Gzip if it's over 1K
|
|
46
|
+
:compression => true,
|
|
31
47
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# +:servers+: Array containing at least one of:
|
|
35
|
-
# MemCache::Server instance
|
|
36
|
-
# Strings like "localhost", "localhost:11211", "localhost:11211:1"
|
|
37
|
-
# That accord to "host:port:weight", only host is required.
|
|
38
|
-
OPTIONS = {
|
|
39
|
-
:multithread => true,
|
|
40
|
-
:readonly => false,
|
|
41
|
-
:servers => ['localhost:11211:1'],
|
|
48
|
+
# Array containing all default Memcache servers
|
|
49
|
+
:servers => ['localhost:11211']
|
|
42
50
|
}
|
|
43
51
|
|
|
44
|
-
#
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
# Hash containing all the default options merged with the user specified
|
|
53
|
+
# ones
|
|
54
|
+
attr_accessor :options
|
|
55
|
+
|
|
56
|
+
class << self
|
|
57
|
+
attr_accessor :options
|
|
58
|
+
|
|
59
|
+
##
|
|
60
|
+
# This method will create a subclass of Ramaze::Cache::MemCache with all
|
|
61
|
+
# the custom options set. All options set in this method will be sent to
|
|
62
|
+
# Dalli as well.
|
|
63
|
+
#
|
|
64
|
+
# Using this method allows you to use different memcache settings for
|
|
65
|
+
# various parts of Ramaze. For example, you might want to use servers A
|
|
66
|
+
# and B for storing the sessions but server C for only views. Most of
|
|
67
|
+
# the way this method works was inspired by Ramaze::Cache::Sequel which
|
|
68
|
+
# was contributed by Lars Olsson.
|
|
69
|
+
#
|
|
70
|
+
# @example
|
|
71
|
+
# Ramaze::Cache.options.session = Ramaze::Cache::MemCache.using(
|
|
72
|
+
# :compression => false,
|
|
73
|
+
# :username => 'ramaze',
|
|
74
|
+
# :password => 'ramaze123',
|
|
75
|
+
# :servers => ['othermachine.com:12345'] # Overwrites the default server
|
|
76
|
+
# )
|
|
77
|
+
#
|
|
78
|
+
# @author Yorick Peterse
|
|
79
|
+
# @since 04-05-2011
|
|
80
|
+
# @param [Hash] options A hash containing all configuration options to
|
|
81
|
+
# use for Dalli. For more information on all the available options you
|
|
82
|
+
# can read the README in their repository. This repository can be found
|
|
83
|
+
# here: https://github.com/mperham/dalli
|
|
84
|
+
#
|
|
85
|
+
def using(options = {})
|
|
86
|
+
merged = Ramaze::Cache::MemCache.trait[:default].merge(options)
|
|
87
|
+
Class.new(self) { @options = merged }
|
|
88
|
+
end
|
|
51
89
|
end
|
|
52
90
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
91
|
+
##
|
|
92
|
+
# Creates a new instance of the cache class.
|
|
93
|
+
#
|
|
94
|
+
# @author Michael Fellinger
|
|
95
|
+
# @since 04-05-2011
|
|
96
|
+
# @param [Hash] options A hash with custom options, see
|
|
97
|
+
# Ramaze::Cache::MemCache.using for all available options.
|
|
98
|
+
#
|
|
99
|
+
def initialize(options = {})
|
|
100
|
+
self.class.options ||= Ramaze::Cache::MemCache.trait[:default].merge(
|
|
101
|
+
options
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
@options = options.merge(self.class.options)
|
|
59
105
|
end
|
|
60
106
|
|
|
107
|
+
##
|
|
108
|
+
# Prepares the cache by creating the namespace and an instance of a Dalli
|
|
109
|
+
# client.
|
|
61
110
|
#
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
111
|
+
# @author Yorick Peterse
|
|
112
|
+
# @since 04-05-2011
|
|
113
|
+
# @param [String] hostname The hostname of the machine running the
|
|
114
|
+
# application.
|
|
115
|
+
# @param [String] username The name of the user executing the process
|
|
116
|
+
# @param [String] appname Unique identifier for the application.
|
|
117
|
+
# @param [String] cachename The namespace to use for this cache instance.
|
|
118
|
+
#
|
|
119
|
+
def cache_setup(hostname, username, appname, cachename)
|
|
120
|
+
# Validate the maximum TTL
|
|
121
|
+
if options[:expires_in] > MAX_TTL
|
|
122
|
+
raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
|
|
123
|
+
end
|
|
68
124
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
value.nil? ? default : value
|
|
75
|
-
rescue ::MemCache::MemCacheError => e
|
|
76
|
-
Log.error(e)
|
|
77
|
-
nil
|
|
125
|
+
options[:namespace] = [
|
|
126
|
+
hostname, username, appname, cachename
|
|
127
|
+
].compact.join('-')
|
|
128
|
+
|
|
129
|
+
@client = ::Dalli::Client.new(options[:servers], options)
|
|
78
130
|
end
|
|
79
131
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
132
|
+
##
|
|
133
|
+
# Removes all items from the cache.
|
|
134
|
+
#
|
|
135
|
+
# @author Yorick Peterse
|
|
136
|
+
# @since 04-05-2011
|
|
137
|
+
#
|
|
138
|
+
def cache_clear
|
|
139
|
+
@client.flush
|
|
140
|
+
end
|
|
89
141
|
|
|
90
|
-
|
|
142
|
+
##
|
|
143
|
+
# Removes the specified keys from the cache.
|
|
144
|
+
#
|
|
145
|
+
# @author Yorick Peterse
|
|
146
|
+
# @since 04-05-2011
|
|
147
|
+
# @param [Array] keys The keys to remove from the cache.
|
|
148
|
+
#
|
|
149
|
+
def cache_delete(*keys)
|
|
150
|
+
keys.each do |key|
|
|
151
|
+
@client.delete(key)
|
|
91
152
|
end
|
|
92
|
-
|
|
93
|
-
@store.set(key, value, ttl)
|
|
94
|
-
value
|
|
95
|
-
rescue ::MemCache::MemCacheError => e
|
|
96
|
-
Log.error(e)
|
|
97
|
-
nil
|
|
98
153
|
end
|
|
99
154
|
|
|
100
|
-
|
|
101
|
-
|
|
155
|
+
##
|
|
156
|
+
# Fetches the specified key from the cache. It the value was nil the
|
|
157
|
+
# default value will be returned instead.
|
|
158
|
+
#
|
|
159
|
+
# @author Yorick Peterse
|
|
160
|
+
# @since 04-05-2011
|
|
161
|
+
# @param [String] key The name of the key to retrieve.
|
|
162
|
+
# @param [Mixed] default The default value.
|
|
163
|
+
# @return [Mixed]
|
|
164
|
+
#
|
|
165
|
+
def cache_fetch(key, default = nil)
|
|
166
|
+
value = @client.get(key)
|
|
102
167
|
|
|
103
|
-
|
|
104
|
-
|
|
168
|
+
if value.nil?
|
|
169
|
+
return default
|
|
170
|
+
else
|
|
171
|
+
return value
|
|
172
|
+
end
|
|
173
|
+
end
|
|
105
174
|
|
|
106
|
-
|
|
107
|
-
|
|
175
|
+
##
|
|
176
|
+
# Sets the given key to the specified value. Optionally you can specify a
|
|
177
|
+
# hash with options specific to the key. Once a key has been stored it's
|
|
178
|
+
# value will be returned.
|
|
179
|
+
#
|
|
180
|
+
# @author Yorick Peterse
|
|
181
|
+
# @since 04-05-2011
|
|
182
|
+
# @param [String] key The name of the key to store.
|
|
183
|
+
# @param [Mixed] value The value to store in Memcache.
|
|
184
|
+
# @param [Fixnum] ttl The Time To Live to use for the current key.
|
|
185
|
+
# @param [Hash] options A hash containing options specific for the
|
|
186
|
+
# specified key.
|
|
187
|
+
# @return [Mixed]
|
|
188
|
+
#
|
|
189
|
+
def cache_store(key, value, ttl = nil, options = {})
|
|
190
|
+
ttl = options.delete(:ttl) || @options[:expires_in]
|
|
108
191
|
|
|
109
|
-
|
|
110
|
-
|
|
192
|
+
if ttl > MAX_TTL
|
|
193
|
+
raise(ArgumentError, "The maximum TTL of Memcache is 30 days")
|
|
194
|
+
end
|
|
111
195
|
|
|
112
|
-
|
|
113
|
-
def compression=(bool); @store.compression = bool; end
|
|
196
|
+
@client.set(key, value, ttl, options)
|
|
114
197
|
|
|
115
|
-
|
|
116
|
-
def method_missing(*args, &block)
|
|
117
|
-
@store.__send__(*args, &block)
|
|
118
|
-
rescue ::MemCache::MemCacheError => e
|
|
119
|
-
Log.error(e)
|
|
120
|
-
nil
|
|
198
|
+
return value
|
|
121
199
|
end
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
end
|
|
200
|
+
end # MemCache
|
|
201
|
+
end # Cache
|
|
202
|
+
end # Ramaze
|