[bknr-cvs] hans changed trunk/projects/pch/src/pch.erl

BKNR Commits bknr at bknr.net
Fri Jul 18 21:34:47 UTC 2008


Revision: 3510
Author: hans
URL: http://bknr.net/trac/changeset/3510

Simple in-memory cache implemented.

U   trunk/projects/pch/src/pch.erl

Modified: trunk/projects/pch/src/pch.erl
===================================================================
--- trunk/projects/pch/src/pch.erl	2008-07-18 16:29:16 UTC (rev 3509)
+++ trunk/projects/pch/src/pch.erl	2008-07-18 21:34:47 UTC (rev 3510)
@@ -2,49 +2,40 @@
 %%% http://www.rsaccon.com/2007/09/mochiweb-erlang-based-webserver-toolkit.html
 
 -module(pch).
--export([start/0, loop/1, stop/0]).
+-export([start/0, loop/2, stop/0]).
 -define(DEFAULTS, [{name, ?MODULE},
                    {port, 9888}]).
 
 start() ->
-    Loop = fun (Req) -> ?MODULE:loop(Req) end,
+    Cache = ets:new(table, [set, public]),
+    Loop = fun (Req) -> ?MODULE:loop(Req, Cache) end,
     mochiweb_http:start([{loop, Loop} | ?DEFAULTS]).
 
 stop() ->
     mochiweb_http:stop(?MODULE).
 
-backend_request(Req) ->
+fetch_from_backend(Path) ->
     {ok, {{_Version, 200, _ReasonPhrase}, Headers, Body}} =
-        http:request("http://test.createrainforest.org" ++ Req:get(path)),
-    Req:respond({200, Headers, Body}).
+        http:request("http://test.createrainforest.org" ++ Path),
+    {ok, Headers, Body}.
 
-loop(Req) ->
-    "/" ++ Path = Req:get(path),
+backend_request(Req, Cache) ->
+    Path = Req:get(path),
+    case ets:lookup(Cache, Path) of
+        [{Path, Headers, Body}] ->
+            io:format("cache HIT for ~p~n", [Path]),
+            Req:respond({200, Headers, Body});
+        _ ->
+            io:format("cache MISS for ~p~n", [Path]),
+            {ok, Headers, Body} = fetch_from_backend(Path),
+            ets:insert(Cache, {Path, Headers, Body}),
+            Req:respond({200, Headers, Body})
+    end.
+
+loop(Req, Cache) ->
     case Req:get(method) of
-        M when M =:= 'GET'; M =:= 'HEAD' ->
-            case Path of
-                "timer" ->
-                    Response = Req:ok({"text/plain", chunked}),
-                    timer(Response);
-                "static" ->
-                    Req:ok({"text/plain", "static response"});
-                "nodes" ->
-                    Req:ok({"text/plain",
-                            io_lib:format("~p~n", [nodes()])});
-                "dump" ->
-                    Req:ok({"text/plain",
-                            io_lib:format("~p~n", [Req:dump()])});
-%%                 _ ->
-%%                     Req:serve_file(Path)
-                _ ->
-                    backend_request(Req)
-            end;
+        M when M =:= 'GET' ->
+            backend_request(Req, Cache);
         _ ->
             Req:respond({501, [], ""})
     end.
-
-timer(Req) ->
-    Req:write_chunk(io_lib:format("The time is: ~p~n",
-                                  [calendar:local_time()])),
-    timer:sleep(1000),
-    timer(Req).




More information about the Bknr-cvs mailing list