Z1maV1

cache.lua

Aug 18th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.86 KB | None | 0 0
  1. cache = {}
  2.  
  3. function cache:new()                -- creates cache class object
  4.     local private = { data = {} }
  5.  
  6.     local obj = {}
  7.  
  8.     function obj:put(key, value)    -- cache value with specified name
  9.         private.data[key] = value
  10.     end
  11.    
  12.     function obj:get(key)           -- get cached value by name or nil
  13.         return private.data[key] or nil
  14.     end
  15.    
  16.     function obj:remove(key)        -- remove value from cache
  17.         private.data[key] = nil
  18.     end
  19.    
  20.     function obj:clear()            -- clear cache
  21.         private.data = {}
  22.     end
  23.  
  24.     function obj:is_present(key)    -- true if value cached, false otherwise
  25.         if private.data[key] then
  26.             return true
  27.         else
  28.             return false
  29.         end
  30.     end
  31.  
  32.     setmetatable(obj, self)
  33.     self.__index = self
  34.     return obj
  35. end
  36.  
  37. return cache
Add Comment
Please, Sign In to add comment