/src/yolk-cache-string_keys.ads

http://github.com/ThomasLocke/yolk · Ada · 110 lines · 24 code · 15 blank · 71 comment · 0 complexity · 960a2d0ffc1fdd61c24f8413037546a9 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. -- As opposed to the Discrete_Keys cache, which maximum size is known at all
  30. -- times, the String_Keys cache carries the risk of growing until there are
  31. -- no more resources available.
  32. -- Either let the cache automatically clean itself (Cleanup_Size and
  33. -- Cleanup_On_Write) or do it manually by calling Cleanup/Clear whenever you
  34. -- decide it is necessary.
  35. --
  36. -- Note that whenever an invalid element is found by the Read procedure,
  37. -- (Is_Valid = False), it is automatically deleted from the cache.
  38. generic
  39. type Element_Type is private;
  40. Cleanup_Size : Positive := 200;
  41. -- Call the Cleanup procedure when the cache contains >= Cleanup_Size
  42. -- elements. The Cleanup procedure first tries to delete elements that are
  43. -- older than Max_Element_Age, and if that doesn't bring the amount of
  44. -- cached elements below Cleanup_Size, then elements are simply _randomly_
  45. -- deleted until the size is 1 lower than Cleanup_Size.
  46. -- Obviously it makes little sense to set this number below the value of
  47. -- Reserved_Capacity.
  48. Cleanup_On_Write : Boolean := True;
  49. -- Call the Cleanup procedure when Write is called and the size of the
  50. -- cache is >= Cleanup_Size. If Cleanup_On_Write is False, Cleanup is not
  51. -- called automatically, and you'll have to manage the size of the cache
  52. -- manually.
  53. Max_Element_Age : Duration := 3600.0;
  54. --- Elements that are older than Max_Element_Age are considered invalid.
  55. Reserved_Capacity : Positive := 100;
  56. -- Set this as close as possible to the expected final size of the cache.
  57. -- Setting it too low will result in a performance-loss whenever the
  58. -- hashed map has to be resized because new key/value pairs are added.
  59. -- Setting it too high will result in wasted resources.
  60. -- This setting has NO bearing on the actual size of the cache. The cache
  61. -- will happily grow beyond the Reserved_Capacity.
  62. package Yolk.Cache.String_Keys is
  63. procedure Cleanup;
  64. -- If the cache size is >= Cleanup_Size, then delete all elements where age
  65. -- is older than Max_Element_Age. If this doesn't bring the size of the
  66. -- cache down below Cleanup_Size, then elements are deleted _randomly_,
  67. -- until the size is Cleanup_Size - 1.
  68. -- Cleanup is called automatically if Cleanup_On_Write is True.
  69. procedure Clear;
  70. -- Clear the entire cache.
  71. procedure Clear
  72. (Key : in String);
  73. -- Remove the currently cached element associated with Key.
  74. function Is_Valid
  75. (Key : in String)
  76. return Boolean;
  77. -- Return True if the element associated with Key exists and is younger
  78. -- than Max_Element_Age.
  79. function Length
  80. return Natural;
  81. -- Return the amount of elements currently in the cache. This counts both
  82. -- valid and invalid elements.
  83. procedure Read
  84. (Key : in String;
  85. Is_Valid : out Boolean;
  86. Value : out Element_Type);
  87. -- Fetch the element associated with Key.
  88. --
  89. -- WARNING!
  90. -- Value will contain undefined garbage if Is_Valid is False.
  91. procedure Write
  92. (Key : in String;
  93. Value : in Element_Type);
  94. -- Add Value to the cache, and associate it with Key.
  95. end Yolk.Cache.String_Keys;