/
e_cache_ets.erl
104 lines (94 loc) · 3.22 KB
/
e_cache_ets.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
%% The contents of this file are subject to the Erlang Web Public License,
%% Version 1.0, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Web Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang-consulting.com/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% The Initial Developer of the Original Code is Erlang Training & Consulting
%% Ltd. Portions created by Erlang Training & Consulting Ltd are Copyright 2008,
%% Erlang Training & Consulting Ltd. All Rights Reserved.
%%%-------------------------------------------------------------------
%%% File : e_cache_ets.erl
%%% @author Michal Ptaszek <info@erlang-consulting.com>
%%% @doc Module responsible for managing the ets cached XML records.
%%% @end
%%%-------------------------------------------------------------------
-module(e_cache_ets).
-export([read_file/2, install/0]).
-export([flush/0]).
%% @hidden
install() ->
ets:new(?MODULE, [named_table, public]).
%%
%% @spec read_file(Filename :: string(), Expander :: atom()) -> term()
%% @doc Reads the file from ets cache.
%% If the file has not been cached or the original one has changed,
%% the cache will be read once again.<br/>
%% When the specified file is missing
%% the <b>erlang:error({Reason, File})</b> is called.
%% @end
%%
-spec(read_file/2 :: (string(), atom()) -> term()).
read_file(File, Expander) ->
Filename = case filelib:is_file(File) of
true ->
File;
false ->
filename:join([e_conf:template_root(), File])
end,
case valid_cache(Filename) of
false ->
cache(Filename, Expander);
Mod when is_atom(Mod) ->
Mod;
CXML ->
binary_to_term(CXML)
end.
%%
%% @spec flush() -> true
%% @doc Removes all the cache entries.
%% Simply invalidates all the cached items.
%% @end
%%
-spec(flush/0 :: () -> true).
flush() ->
ets:delete_all_objects(?MODULE).
-spec(valid_cache/1 :: (string()) -> false | binary()).
valid_cache(File) ->
case ets:lookup(?MODULE, File) of
[{_, Stamp, Body}] ->
case filelib:last_modified(File) > Stamp of
true ->
false;
false ->
Body
end;
[] ->
false
end.
-spec(cache/2 :: (string(), atom()) -> term()).
cache(File, wpart_xs) ->
XML = case xmerl_scan:file(File, []) of
{error, Reason} ->
erlang:error(Reason);
{XML3, _} ->
XML3
end,
ets:insert(?MODULE, {File, {date(), time()}, term_to_binary(XML)}),
XML;
cache(File, erlydtl_expander) ->
Mod = list_to_atom(string:join(string:tokens(File, "/.-"), "")),
case erlydtl:compile(File, Mod) of
ok ->
ets:insert(?MODULE, {File, {date(), time()}, Mod}),
Mod;
{error, Reason} ->
erlang:error(Reason)
end;
cache(_File, Expander) ->
erlang:error({unknown_expander, Expander}).