/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 ¸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. -- A simple and fairly dumb cache.
  23. -- In order for an element to be valid, it must:
  24. --
  25. -- 1. have been added to the cache using the Write procedure
  26. -- 2. be younger than Max_Element_Age
  27. --
  28. -- WARNING!
  29. -- If your Key_Type is for example an Integer, then the cache _can_ grow to
  30. -- whatever the size of Integer is on your implementation. Obviously this
  31. -- can potentially use a lot of resources, so give some thought to Key_Type
  32. -- before you dump any old discrete type in there.
  33. --
  34. -- Note that whenever an invalid element is found by the Read procedure
  35. -- (Is_Valid = False), it is automatically deleted from the cache.
  36. generic
  37. type Key_Type is (<>);
  38. type Element_Type is private;
  39. Max_Element_Age : Duration := 3600.0;
  40. -- Elements that are older than Max_Element_Age are considered invalid.
  41. package Yolk.Cache.Discrete_Keys is
  42. procedure Cleanup;
  43. -- Clear all stale elements from the cache. Basically this iterates over
  44. -- every single object in the cache and deletes it if it is older than
  45. -- Max_Element_Age.
  46. -- Obviously this is a very expensive call if the cache is large. Use with
  47. -- care.
  48. procedure Clear;
  49. -- Clear the entire cache.
  50. procedure Clear
  51. (Key : in Key_Type);
  52. -- Remove the currently cached element associated with Key.
  53. function Is_Valid
  54. (Key : in Key_Type)
  55. return Boolean;
  56. -- Return True if the element associated with Key exists and is younger
  57. -- than Max_Element_Age.
  58. function Length
  59. return Natural;
  60. -- Return the amount of elements currently in the cache. This counts both
  61. -- valid and invalid elements.
  62. procedure Read
  63. (Key : in Key_Type;
  64. Is_Valid : out Boolean;
  65. Value : out Element_Type);
  66. -- Fetch the element associated with Key.
  67. --
  68. -- WARNING!
  69. -- Value will contain undefined garbage if Is_Valid is False.
  70. procedure Write
  71. (Key : in Key_Type;
  72. Value : in Element_Type);
  73. -- Add Value to the cache, and associate it with Key.
  74. end Yolk.Cache.Discrete_Keys;