PageRenderTime 31ms CodeModel.GetById 23ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/src/yolk-cache-discrete_keys.ads

http://github.com/ThomasLocke/yolk
Ada | 86 lines | 22 code | 12 blank | 52 comment | 0 complexity | 732809aa07d12c5dca61649c350234ad MD5 | raw file
 1-------------------------------------------------------------------------------
 2--                                                                           --
 3--                   Copyright (C) 2010-, Thomas Løcke                   --
 4--                                                                           --
 5--  This library is free software;  you can redistribute it and/or modify    --
 6--  it under terms of the  GNU General Public License  as published by the   --
 7--  Free Software  Foundation;  either version 3,  or (at your  option) any  --
 8--  later version. This library is distributed in the hope that it will be   --
 9--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of  --
10--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
11--                                                                           --
12--  As a special exception under Section 7 of GPL version 3, you are         --
13--  granted additional permissions described in the GCC Runtime Library      --
14--  Exception, version 3.1, as published by the Free Software Foundation.    --
15--                                                                           --
16--  You should have received a copy of the GNU General Public License and    --
17--  a copy of the GCC Runtime Library Exception along with this program;     --
18--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
19--  <http://www.gnu.org/licenses/>.                                          --
20--                                                                           --
21-------------------------------------------------------------------------------
22
23--  A simple and fairly dumb cache.
24--  In order for an element to be valid, it must:
25--
26--    1. have been added to the cache using the Write procedure
27--    2. be younger than Max_Element_Age
28--
29--  WARNING!
30--    If your Key_Type is for example an Integer, then the cache _can_ grow to
31--    whatever the size of Integer is on your implementation. Obviously this
32--    can potentially use a lot of resources, so give some thought to Key_Type
33--    before you dump any old discrete type in there.
34--
35--  Note that whenever an invalid element is found by the Read procedure
36--  (Is_Valid = False), it is automatically deleted from the cache.
37
38generic
39
40   type Key_Type is (<>);
41   type Element_Type is private;
42   Max_Element_Age : Duration := 3600.0;
43   --  Elements that are older than Max_Element_Age are considered invalid.
44
45package Yolk.Cache.Discrete_Keys is
46
47   procedure Cleanup;
48   --  Clear all stale elements from the cache. Basically this iterates over
49   --  every single object in the cache and deletes it if it is older than
50   --  Max_Element_Age.
51   --  Obviously this is a very expensive call if the cache is large. Use with
52   --  care.
53
54   procedure Clear;
55   --  Clear the entire cache.
56
57   procedure Clear
58     (Key : in Key_Type);
59   --  Remove the currently cached element associated with Key.
60
61   function Is_Valid
62     (Key : in Key_Type)
63      return Boolean;
64   --  Return True if the element associated with Key exists and is younger
65   --  than Max_Element_Age.
66
67   function Length
68     return Natural;
69   --  Return the amount of elements currently in the cache. This counts both
70   --  valid and invalid elements.
71
72   procedure Read
73     (Key      : in  Key_Type;
74      Is_Valid : out Boolean;
75      Value    : out Element_Type);
76   --  Fetch the element associated with Key.
77   --
78   --  WARNING!
79   --    Value will contain undefined garbage if Is_Valid is False.
80
81   procedure Write
82     (Key   : in Key_Type;
83      Value : in Element_Type);
84   --  Add Value to the cache, and associate it with Key.
85
86end Yolk.Cache.Discrete_Keys;