4
%% Copyright Ericsson AB 2008-2009. All Rights Reserved.
4
%% Copyright Ericsson AB 2008-2010. All Rights Reserved.
6
6
%% The contents of this file are subject to the Erlang Public License,
7
7
%% Version 1.1, (the "License"); you may not use this file except in
8
8
%% compliance with the License. You should have received a copy of the
9
9
%% Erlang Public License along with this software. If not, it can be
10
10
%% retrieved online at http://www.erlang.org/.
12
12
%% Software distributed under the License is distributed on an "AS IS"
13
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
14
%% the License for the specific language governing rights and limitations
15
15
%% under the License.
23
23
-behaviour(ssl_session_cache_api).
25
-export([init/0, terminate/1, lookup/2, update/3, delete/2, foldl/3,
25
-include("ssl_handshake.hrl").
26
-include("ssl_internal.hrl").
28
-export([init/1, terminate/1, lookup/2, update/3, delete/2, foldl/3,
31
-type key() :: {{host(), port_num()}, session_id()} | {port_num(), session_id()}.
28
33
%%--------------------------------------------------------------------
29
%% Function: init() -> Cache
31
%% Cache - Reference to the cash (opaque)
34
-spec init(list()) -> cache_ref(). %% Returns reference to the cache (opaque)
33
36
%% Description: Return table reference. Called by ssl_manager process.
34
37
%%--------------------------------------------------------------------
36
39
ets:new(cache_name(), [set, protected]).
38
41
%%--------------------------------------------------------------------
39
%% Function: terminate(Cache) ->
41
%% Cache - as returned by create/0
42
-spec terminate(cache_ref()) -> any(). %%
43
44
%% Description: Handles cache table at termination of ssl manager.
44
45
%%--------------------------------------------------------------------
48
49
%%--------------------------------------------------------------------
49
%% Function: lookup(Cache, Key) -> Session | undefined
50
%% Cache - as returned by create/0
51
%% Session = #session{}
50
-spec lookup(cache_ref(), key()) -> #session{} | undefined.
53
52
%% Description: Looks up a cach entry. Should be callable from any
64
63
%%--------------------------------------------------------------------
65
%% Function: update(Cache, Key, Session) -> _
66
%% Cache - as returned by create/0
67
%% Session = #session{}
64
-spec update(cache_ref(), key(), #session{}) -> any().
69
66
%% Description: Caches a new session or updates a already cached one.
70
67
%% Will only be called from the ssl_manager process.
73
70
ets:insert(Cache, {Key, Session}).
75
72
%%--------------------------------------------------------------------
76
%% Function: delete(Cache, HostIP, Port, Id) -> _
77
%% Cache - as returned by create/0
78
%% HostIP = Host = string() | ipadress()
73
-spec delete(cache_ref(), key()) -> any().
82
75
%% Description: Delets a cache entry.
83
76
%% Will only be called from the ssl_manager process.
86
79
ets:delete(Cache, Key).
88
81
%%--------------------------------------------------------------------
89
%% Function: foldl(Fun, Acc0, Cache) -> Acc
93
%% Cache - cache_ref()
82
-spec foldl(fun(), term(), cache_ref()) -> term().
96
84
%% Description: Calls Fun(Elem, AccIn) on successive elements of the
97
85
%% cache, starting with AccIn == Acc0. Fun/2 must return a new
98
86
%% accumulator which is passed to the next call. The function returns
99
%% the final value of the accumulator. Acc0 is returned if the cache is
101
%% Should be callable from any process
87
%% the final value of the accumulator. Acc0 is returned if the cache
88
%% is empty.Should be callable from any process
102
89
%%--------------------------------------------------------------------
103
90
foldl(Fun, Acc0, Cache) ->
104
91
ets:foldl(Fun, Acc0, Cache).
106
93
%%--------------------------------------------------------------------
107
%% Function: select_session(Cache, PartialKey) -> [Sessions]
109
%% Cache - as returned by create/0
110
%% PartialKey - opaque Key = {PartialKey, SessionId}
94
-spec select_session(cache_ref(), {host(), port_num()} | port_num()) -> [#session{}].
112
96
%% Description: Selects a session that could be reused. Should be callable
113
97
%% from any process.