manveru-ramaze 2009.04.01 → 2009.04.08

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/CHANGELOG +201 -0
  2. data/MANIFEST +10 -8
  3. data/Rakefile +19 -3
  4. data/bin/ramaze +142 -55
  5. data/doc/tutorial/todolist.html +83 -5
  6. data/doc/tutorial/todolist.txt +85 -33
  7. data/examples/app/blog/start.rb +1 -1
  8. data/examples/app/todolist/model/init.rb +3 -0
  9. data/examples/templates/template_redcloth.rb +2 -2
  10. data/lib/proto/layout/{default.nag → default.xhtml} +0 -0
  11. data/lib/proto/view/{index.nag → index.xhtml} +0 -0
  12. data/lib/proto/view/{page.nag → page.xhtml} +0 -0
  13. data/lib/ramaze/app.rb +5 -5
  14. data/lib/ramaze/cache.rb +3 -2
  15. data/lib/ramaze/cache/localmemcache.rb +56 -0
  16. data/lib/ramaze/contrib/app_graph.rb +64 -0
  17. data/lib/ramaze/controller/default.rb +5 -0
  18. data/lib/ramaze/helper.rb +6 -16
  19. data/lib/ramaze/helper/httpdigest.rb +1 -1
  20. data/lib/ramaze/helper/link.rb +2 -5
  21. data/lib/ramaze/helper/partial.rb +85 -10
  22. data/lib/ramaze/request.rb +20 -0
  23. data/lib/ramaze/snippets/ramaze/deprecated.rb +0 -1
  24. data/lib/ramaze/spec.rb +10 -0
  25. data/lib/ramaze/tool/create.rb +48 -0
  26. data/lib/ramaze/tool/project_creator.rb +111 -0
  27. data/lib/ramaze/version.rb +1 -1
  28. data/ramaze.gemspec +3 -9
  29. data/spec/examples/templates/template_markaby.rb +1 -1
  30. data/spec/ramaze/cache/localmemcache.rb +49 -0
  31. data/spec/ramaze/helper/sequel_form.rb +1 -1
  32. data/tasks/bacon.rake +38 -21
  33. metadata +12 -31
  34. data/examples/templates/template_xslt.rb +0 -48
  35. data/examples/templates/view/external.xsl +0 -57
  36. data/lib/ramaze/contrib/sequel/fill.rb +0 -12
  37. data/spec/contrib/sequel/fill.rb +0 -47
  38. data/spec/examples/templates/template_xslt.rb +0 -10
@@ -3,7 +3,7 @@
3
3
  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4
4
  <head>
5
5
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
6
- <meta name="generator" content="AsciiDoc 8.4.1" />
6
+ <meta name="generator" content="AsciiDoc 8.4.2" />
7
7
  <title>The official Ramaze Todo-list tutorial</title>
8
8
  <style type="text/css">
9
9
  /* Debug borders */
@@ -448,6 +448,12 @@ either on the <a href="http://ramazetrac.purepistos.org/">Bug tracker</a>, or ju
448
448
  drop by on IRC on <tt>irc.freenode.org</tt> in the channel <tt>#ramaze</tt>.</p></div>
449
449
  <div class="paragraph"><p>If you have trouble with some of the terms used in this tutorial you can
450
450
  consult the <a href="#glossary">Glossary</a> at the end of this document.</p></div>
451
+ <div class="paragraph"><p>We are also working on a book that describes Ramaze in more depth, called
452
+ <em>Journey to Ramaze</em>, it is still very much work in progress, but some of the
453
+ contents might interest you.</p></div>
454
+ <div class="paragraph"><p>The repository for the book is at <a href="http://github.com/manveru/ramaze-book">http://github.com/manveru/ramaze-book</a>.
455
+ Every once in a while, updates for the book will be available in HTML and PDF
456
+ form at <a href="http://book.ramaze.net">http://book.ramaze.net</a>.</p></div>
451
457
  </div>
452
458
  <h2 id="_first_step_create">First Step, Create</h2>
453
459
  <div class="sectionbody">
@@ -521,7 +527,7 @@ Ramaze<span style="color: #990000">.</span>start</tt></pre></div></div>
521
527
  <pre><tt>delta ~/tmp/tutorial % ruby start.rb
522
528
  D [2009-03-30 14:15:01 $2124] DEBUG | : Using webrick
523
529
  I [2009-03-30 14:15:01 $2124] INFO | : WEBrick 1.3.1
524
- I [2009-03-30 14:15:01 $2124] INFO | : ruby 1.8.7 (2008-08-11) [i686-linux]
530
+ I [2009-03-30 14:15:01 $2124] INFO | : ruby 1.9.2 (2009-03-02) [i686-linux]
525
531
  D [2009-03-30 14:15:01 $2124] DEBUG | : TCPServer.new(0.0.0.0, 7000)
526
532
  D [2009-03-30 14:15:01 $2124] DEBUG | : Rack::Handler::WEBrick is mounted on /.
527
533
  I [2009-03-30 14:15:01 $2124] INFO | : WEBrick::HTTPServer#start: pid=2124 port=7000</tt></pre>
@@ -552,8 +558,7 @@ you tremendously when it comes to writing your own applications.</p></div>
552
558
  This requires the <a href="http://rubyforge.org/projects/sqlite-ruby">sqlite-ruby</a>
553
559
  bindings.</p></div>
554
560
  <div class="paragraph"><p>You can try to <tt>gem install sqlite</tt>, which will complain if your system doesn&#8217;t
555
- provide bindings, in which case I have to refer you to
556
- TODO <a href="http://sqlite.rubyforge.org">http://sqlite.rubyforge.org</a></p></div>
561
+ provide bindings, in which case I have to refer you to <a href="http://sqlite.org">http://sqlite.org</a>.</p></div>
557
562
  <div class="paragraph"><p>In order to use Sequel we also need a database connection.</p></div>
558
563
  <div class="paragraph"><p>So we create a new file at <em>model/init.rb</em> with following content:</p></div>
559
564
  <div class="listingblock">
@@ -563,7 +568,11 @@ http://www.lorenzobettini.it
563
568
  http://www.gnu.org/software/src-highlite -->
564
569
  <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sequel'</span>
565
570
 
571
+ Sequel<span style="color: #990000">::</span>Model<span style="color: #990000">.</span>plugin<span style="color: #990000">(:</span>schema<span style="color: #990000">)</span>
572
+
566
573
  DB <span style="color: #990000">=</span> Sequel<span style="color: #990000">.</span>sqlite<span style="color: #990000">(</span><span style="color: #FF0000">'todolist.db'</span><span style="color: #990000">)</span></tt></pre></div></div>
574
+ <div class="paragraph"><p>The <tt>:schema</tt> plugin is required since Sequel 3.0, if you run a version prior
575
+ to 2.12 you may remove this line if it gives you any problems.</p></div>
567
576
  <div class="paragraph"><p>Next we edit <em>start.rb</em>, remove the <tt>Hello</tt> class, and add a require for the
568
577
  file, <em>start.rb</em> should look like this now:</p></div>
569
578
  <div class="listingblock">
@@ -611,6 +620,8 @@ http://www.lorenzobettini.it
611
620
  http://www.gnu.org/software/src-highlite -->
612
621
  <pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'sequel'</span>
613
622
 
623
+ Sequel<span style="color: #990000">::</span>Model<span style="color: #990000">.</span>plugin<span style="color: #990000">(:</span>schema<span style="color: #990000">)</span>
624
+
614
625
  DB <span style="color: #990000">=</span> Sequel<span style="color: #990000">.</span>sqlite<span style="color: #990000">(</span><span style="color: #FF0000">'todolist.db'</span><span style="color: #990000">)</span>
615
626
 
616
627
  <span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'model/task'</span></tt></pre></div></div>
@@ -1255,6 +1266,73 @@ http://www.gnu.org/software/src-highlite -->
1255
1266
  <div class="paragraph"><p>Voilà, you now have acquired the Certificate of Ramazeness and all your friends
1256
1267
  and enemies envy you.</p></div>
1257
1268
  </div>
1269
+ <h2 id="_twelfth_step_configuring_configurable_configurability">Twelfth Step, configuring configurable configurability</h2>
1270
+ <div class="sectionbody">
1271
+ <div class="paragraph"><p>To round up this tutorial a bit, let&#8217;s introduce you to configuration in Ramaze.
1272
+ There are a number of ways to configure Ramaze, but here we&#8217;ll just see the
1273
+ most common ones with some options you&#8217;ll most likely want to change.</p></div>
1274
+ <div class="paragraph"><p>First of all, you have been running your ramaze application always on the same
1275
+ port, <tt>7000</tt>, which prevents you from starting more than one instance or other
1276
+ applications.</p></div>
1277
+ <div class="paragraph"><p>To change the port, you can, for example:</p></div>
1278
+ <div class="listingblock">
1279
+ <div class="content"><!-- Generator: GNU source-highlight 2.11.1
1280
+ by Lorenzo Bettini
1281
+ http://www.lorenzobettini.it
1282
+ http://www.gnu.org/software/src-highlite -->
1283
+ <pre><tt>Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>adapter<span style="color: #990000">.</span>port <span style="color: #990000">=</span> <span style="color: #993399">80</span></tt></pre></div></div>
1284
+ <div class="admonitionblock">
1285
+ <table><tr>
1286
+ <td class="icon">
1287
+ <div class="title">Note</div>
1288
+ </td>
1289
+ <td class="content">Running a server on a port below 1024 will require root privileges and is
1290
+ generally not advised for applications that don&#8217;t drop their privileges
1291
+ after establishing a connection.
1292
+ Please have a look at <a href="http://wiki.ramaze.net/Deployment">http://wiki.ramaze.net/Deployment</a> for better ways
1293
+ to deploy your site using a reverse proxy like apache, lighttpd, or
1294
+ nginx.</td>
1295
+ </tr></table>
1296
+ </div>
1297
+ <div class="paragraph"><p>OK, a different port is fine, but how about some speed-boost? For this we will
1298
+ need a faster server like <a href="http://mongrel.rubyforge.org">Mongrel</a> or
1299
+ <a href="http://thin.rubyforge.org">Thin</a>.</p></div>
1300
+ <div class="paragraph"><p>You can install either one via:</p></div>
1301
+ <div class="listingblock">
1302
+ <div class="content">
1303
+ <pre><tt>gem install thin
1304
+ gem install mongrel</tt></pre>
1305
+ </div></div>
1306
+ <div class="paragraph"><p>Now to the configuration:</p></div>
1307
+ <div class="listingblock">
1308
+ <div class="content"><!-- Generator: GNU source-highlight 2.11.1
1309
+ by Lorenzo Bettini
1310
+ http://www.lorenzobettini.it
1311
+ http://www.gnu.org/software/src-highlite -->
1312
+ <pre><tt><span style="font-style: italic"><span style="color: #9A1900"># The default is WEBrick</span></span>
1313
+ Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>adapter<span style="color: #990000">.</span>adapter <span style="color: #990000">=</span> <span style="color: #990000">:</span>webrick
1314
+
1315
+ <span style="font-style: italic"><span style="color: #9A1900"># How about using Mongrel instead?</span></span>
1316
+ Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>adapter<span style="color: #990000">.</span>adapter <span style="color: #990000">=</span> <span style="color: #990000">:</span>mongrel
1317
+
1318
+ <span style="font-style: italic"><span style="color: #9A1900"># Or maybe Thin?</span></span>
1319
+ Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>adapter<span style="color: #990000">.</span>adapter <span style="color: #990000">=</span> <span style="color: #990000">:</span>thin</tt></pre></div></div>
1320
+ <div class="paragraph"><p>For the full performance, switch Ramaze into <tt>:live</tt> mode:</p></div>
1321
+ <div class="listingblock">
1322
+ <div class="content"><!-- Generator: GNU source-highlight 2.11.1
1323
+ by Lorenzo Bettini
1324
+ http://www.lorenzobettini.it
1325
+ http://www.gnu.org/software/src-highlite -->
1326
+ <pre><tt><span style="font-style: italic"><span style="color: #9A1900"># The default is :dev</span></span>
1327
+ Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>mode <span style="color: #990000">=</span> <span style="color: #990000">:</span>live
1328
+
1329
+ <span style="font-style: italic"><span style="color: #9A1900"># And here comes :live</span></span>
1330
+ Ramaze<span style="color: #990000">.</span>options<span style="color: #990000">.</span>mode <span style="color: #990000">=</span> <span style="color: #990000">:</span>live</tt></pre></div></div>
1331
+ <div class="paragraph"><p>The major differences between <tt>:dev</tt> and <tt>:live</tt> are that in <tt>:live</tt> mode your
1332
+ code won&#8217;t be automatically reloaded if it has changed and we don&#8217;t run every
1333
+ request through <tt>Rack::Lint</tt>, which helps you to stay within the
1334
+ request/response specifications required by Rack.</p></div>
1335
+ </div>
1258
1336
  <h2 id="glossary">Glossary</h2>
1259
1337
  <div class="sectionbody">
1260
1338
  <div class="dlist glossary"><dl>
@@ -1330,7 +1408,7 @@ RSI
1330
1408
  <div id="footer">
1331
1409
  <div id="footer-text">
1332
1410
  Version 2.0<br />
1333
- Last updated 2009-03-30 20:29:25 JST
1411
+ Last updated 2009-04-07 19:08:12 JST
1334
1412
  </div>
1335
1413
  </div>
1336
1414
  </body>
@@ -32,6 +32,15 @@ drop by on IRC on `irc.freenode.org` in the channel `#ramaze`.
32
32
  If you have trouble with some of the terms used in this tutorial you can
33
33
  consult the <<glossary, Glossary>> at the end of this document.
34
34
 
35
+ We are also working on a book that describes Ramaze in more depth, called
36
+ 'Journey to Ramaze', it is still very much work in progress, but some of the
37
+ contents might interest you.
38
+
39
+ The repository for the book is at http://github.com/manveru/ramaze-book.
40
+ Every once in a while, updates for the book will be available in HTML and PDF
41
+ form at http://book.ramaze.net.
42
+
43
+
35
44
  == First Step, Create
36
45
 
37
46
  The last version of this tutorial assumed a generator to produce a skeleton in
@@ -87,7 +96,7 @@ tradition, we create a file at the root of your application directory called
87
96
  require 'rubygems'
88
97
  require 'ramaze'
89
98
 
90
- class Hello < Ramaze::Controller
99
+ class MainController < Ramaze::Controller
91
100
  def index
92
101
  "Hello, World!"
93
102
  end
@@ -102,7 +111,7 @@ Now we run it:
102
111
  delta ~/tmp/tutorial % ruby start.rb
103
112
  D [2009-03-30 14:15:01 $2124] DEBUG | : Using webrick
104
113
  I [2009-03-30 14:15:01 $2124] INFO | : WEBrick 1.3.1
105
- I [2009-03-30 14:15:01 $2124] INFO | : ruby 1.8.7 (2008-08-11) [i686-linux]
114
+ I [2009-03-30 14:15:01 $2124] INFO | : ruby 1.9.2 (2009-03-02) [i686-linux]
106
115
  D [2009-03-30 14:15:01 $2124] DEBUG | : TCPServer.new(0.0.0.0, 7000)
107
116
  D [2009-03-30 14:15:01 $2124] DEBUG | : Rack::Handler::WEBrick is mounted on /.
108
117
  I [2009-03-30 14:15:01 $2124] INFO | : WEBrick::HTTPServer#start: pid=2124 port=7000
@@ -141,8 +150,7 @@ This requires the http://rubyforge.org/projects/sqlite-ruby[sqlite-ruby]
141
150
  bindings.
142
151
 
143
152
  You can try to `gem install sqlite`, which will complain if your system doesn't
144
- provide bindings, in which case I have to refer you to
145
- TODO http://sqlite.rubyforge.org
153
+ provide bindings, in which case I have to refer you to http://sqlite.org.
146
154
 
147
155
  In order to use Sequel we also need a database connection.
148
156
 
@@ -152,9 +160,14 @@ So we create a new file at 'model/init.rb' with following content:
152
160
  --------------------------------------------------------------------------------
153
161
  require 'sequel'
154
162
 
163
+ Sequel::Model.plugin(:schema)
164
+
155
165
  DB = Sequel.sqlite('todolist.db')
156
166
  --------------------------------------------------------------------------------
157
167
 
168
+ The `:schema` plugin is required since Sequel 3.0, if you run a version prior
169
+ to 2.12 you may remove this line if it gives you any problems.
170
+
158
171
  Next we edit 'start.rb', remove the `Hello` class, and add a require for the
159
172
  file, 'start.rb' should look like this now:
160
173
 
@@ -202,6 +215,8 @@ Finally, add a line to your 'model/init.rb' that requires 'model/task.rb':
202
215
  --------------------------------------------------------------------------------
203
216
  require 'sequel'
204
217
 
218
+ Sequel::Model.plugin(:schema)
219
+
205
220
  DB = Sequel.sqlite('todolist.db')
206
221
 
207
222
  require 'model/task'
@@ -832,35 +847,72 @@ What we do cover is adding it to your application, or the `<head>` in
832
847
  Voilà, you now have acquired the Certificate of Ramazeness and all your friends
833
848
  and enemies envy you.
834
849
 
835
- ////
836
- That's just some experimental stuff, uncomment and create an 'image' folder to
837
- use it.
838
-
839
- [graphviz,"image/relations.png","dot"]
840
- ---------------------------------------------------------------------
841
- digraph relations {
842
- "start.rb" -> "controller/task.rb";
843
- "start.rb" -> "model/init.rb";
844
- "model/init.rb" -> "model/task.rb";
845
- "controller/task.rb" -> "layout/default.xhtml";
846
- "controller/task.rb" -> "view/index.xhtml";
847
- "controller/task.rb" -> "view/done.xhtml";
848
- "layout/default.xhtml" -> "public/css/screen.css";
849
-
850
- "Tasks Controller" -> "create action";
851
- "Tasks Controller" -> "index action";
852
- "Tasks Controller" -> "open action";
853
- "Tasks Controller" -> "done action";
854
- "Tasks Controller" -> "close action";
855
- "Tasks Controller" -> "delete action";
856
-
857
- "done action" -> "Task.filter(:done => true)";
858
- "index action" -> "Task.filter(:done => false)";
859
- }
860
- ---------------------------------------------------------------------
861
-
862
- image:image/relations.png[Relations of parts of the application]
863
- ////
850
+
851
+ == Twelfth Step, configuring configurable configurability
852
+
853
+ To round up this tutorial a bit, let's introduce you to configuration in Ramaze.
854
+ There are a number of ways to configure Ramaze, but here we'll just see the
855
+ most common ones with some options you'll most likely want to change.
856
+
857
+ First of all, you have been running your ramaze application always on the same
858
+ port, `7000`, which prevents you from starting more than one instance or other
859
+ applications.
860
+
861
+ To change the port, you can, for example:
862
+
863
+ [source,ruby]
864
+ --------------------------------------------------------------------------------
865
+ Ramaze.options.adapter.port = 80
866
+ --------------------------------------------------------------------------------
867
+
868
+ NOTE: Running a server on a port below 1024 will require root privileges and is
869
+ generally not advised for applications that don't drop their privileges
870
+ after establishing a connection.
871
+ Please have a look at http://wiki.ramaze.net/Deployment for better ways
872
+ to deploy your site using a reverse proxy like apache, lighttpd, or
873
+ nginx.
874
+
875
+ OK, a different port is fine, but how about some speed-boost? For this we will
876
+ need a faster server like http://mongrel.rubyforge.org[Mongrel] or
877
+ http://thin.rubyforge.org[Thin].
878
+
879
+ You can install either one via:
880
+
881
+ --------------------------------------------------------------------------------
882
+ gem install thin
883
+ gem install mongrel
884
+ --------------------------------------------------------------------------------
885
+
886
+ Now to the configuration:
887
+
888
+ [source,ruby]
889
+ --------------------------------------------------------------------------------
890
+ # The default is WEBrick
891
+ Ramaze.options.adapter.adapter = :webrick
892
+
893
+ # How about using Mongrel instead?
894
+ Ramaze.options.adapter.adapter = :mongrel
895
+
896
+ # Or maybe Thin?
897
+ Ramaze.options.adapter.adapter = :thin
898
+ --------------------------------------------------------------------------------
899
+
900
+ For the full performance, switch Ramaze into `:live` mode:
901
+
902
+ [source,ruby]
903
+ --------------------------------------------------------------------------------
904
+ # The default is :dev
905
+ Ramaze.options.mode = :live
906
+
907
+ # And here comes :live
908
+ Ramaze.options.mode = :live
909
+ --------------------------------------------------------------------------------
910
+
911
+ The major differences between `:dev` and `:live` are that in `:live` mode your
912
+ code won't be automatically reloaded if it has changed and we don't run every
913
+ request through `Rack::Lint`, which helps you to stay within the
914
+ request/response specifications required by Rack.
915
+
864
916
 
865
917
  [[glossary]]
866
918
  == Glossary
@@ -2,4 +2,4 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
 
3
3
  require 'app'
4
4
 
5
- Ramaze.start(:adapter => :webrick, :mode => :dev)
5
+ Ramaze.start(:adapter => :webrick, :mode => :dev) if $0 == __FILE__
@@ -1,4 +1,7 @@
1
1
  module TodoList
2
+ # yeah, we're lazy
3
+ Sequel::Model.plugin(:schema)
4
+
2
5
  # Uncomment the line for the DB you want to use.
3
6
 
4
7
  # Sqlite In memory, fastest, but cannot persist over restarts.
@@ -42,13 +42,13 @@ __REDCLOTH__
42
42
  end
43
43
 
44
44
  def layout
45
- <<__HTML__
45
+ <<'__HTML__'
46
46
  <html>
47
47
  <head>
48
48
  <title>Template::RedCloth <%= @place %></title>
49
49
  </head>
50
50
  <body>
51
- #{@content}
51
+ <%= @content %>
52
52
  </body>
53
53
  </html>
54
54
  __HTML__
File without changes
File without changes
File without changes
data/lib/ramaze/app.rb CHANGED
@@ -68,11 +68,11 @@ module Ramaze
68
68
 
69
69
  APP_LIST = {}
70
70
 
71
- attr_reader :name, :location, :map, :options
71
+ attr_reader :name, :location, :url_map, :options
72
72
 
73
73
  def initialize(name, location)
74
74
  @name = name.to_sym
75
- @map = Innate::URLMap.new
75
+ @url_map = Innate::URLMap.new
76
76
  self.location = location
77
77
 
78
78
  APP_LIST[@name] = self
@@ -95,16 +95,16 @@ module Ramaze
95
95
 
96
96
  def to_app
97
97
  files = Ramaze::Files.new(*public_roots)
98
- app = Current.new(Route.new(@map), Rewrite.new(@map))
98
+ app = Current.new(Route.new(url_map), Rewrite.new(url_map))
99
99
  Rack::Cascade.new([files, app])
100
100
  end
101
101
 
102
102
  def map(location, object)
103
- @map.map(location, object)
103
+ url_map.map(location, object)
104
104
  end
105
105
 
106
106
  def to(object)
107
- return unless mapped = @map.to(object)
107
+ return unless mapped = url_map.to(object)
108
108
  [location, mapped].join('/').squeeze('/')
109
109
  end
110
110
 
data/lib/ramaze/cache.rb CHANGED
@@ -7,8 +7,9 @@ module Ramaze
7
7
  Cache = Innate::Cache
8
8
 
9
9
  class Cache
10
- autoload :MemCache, 'ramaze/cache/memcache'
11
- autoload :Sequel, 'ramaze/cache/sequel'
10
+ autoload :LocalMemCache, 'ramaze/cache/localmemcache'
11
+ autoload :MemCache, 'ramaze/cache/memcache'
12
+ autoload :Sequel, 'ramaze/cache/sequel'
12
13
 
13
14
  def self.clear_after_reload
14
15
  action.clear if respond_to?(:action)
@@ -0,0 +1,56 @@
1
+ # Copyright (c) 2009 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require 'localmemcache'
5
+
6
+ module Ramaze
7
+ class Cache
8
+
9
+ # Cache based on the localmemcache library which utilizes mmap to share
10
+ # strings in memory between ruby instances.
11
+ class LocalMemCache
12
+ include Cache::API
13
+
14
+ OPTIONS = {
15
+ :size_mb => 1024,
16
+ :serialize => true,
17
+ :serializer => ::Marshal, # something that responds to ::load and ::dump
18
+ }
19
+
20
+ # Connect to localmemcache
21
+ def cache_setup(host, user, app, name)
22
+ @namespace = [host, user, app, name].compact.join('-')
23
+
24
+ options = {:namespace => @namespace}.merge(OPTIONS)
25
+
26
+ @serialize = options.delete(:serialize)
27
+ @serializer = options.delete(:serializer)
28
+
29
+ @store = ::LocalMemCache.new(options)
30
+ end
31
+
32
+ # Wipe out _all_ data in localmemcached, use with care.
33
+ def cache_clear
34
+ ::LocalMemCache.clear_namespace(@namespace)
35
+ end
36
+
37
+ def cache_delete(*args)
38
+ super{|key| @store.delete(key.to_s) }
39
+ end
40
+
41
+ # NOTE:
42
+ # * We have no way of knowing whether the value really is nil, we
43
+ # assume you wouldn't cache nil and return the default instead.
44
+ def cache_fetch(*args)
45
+ super{|key|
46
+ value = @store[key.to_s]
47
+ @serializer.load(value) if value
48
+ }
49
+ end
50
+
51
+ def cache_store(*args)
52
+ super{|key, value| @store[key.to_s] = @serializer.dump(value) }
53
+ end
54
+ end
55
+ end
56
+ end