Skip to content

Commit c43a933

Browse files
committed
chore: update resty.lrucache annotations
1 parent 31eb51d commit c43a933

File tree

2 files changed

+131
-16
lines changed

2 files changed

+131
-16
lines changed
Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,115 @@
11
---@meta
2-
resty_lrucache={}
3-
function resty_lrucache.delete(self, key) end
4-
function resty_lrucache.new(size) end
5-
function resty_lrucache.set(self, key, value, ttl) end
6-
function resty_lrucache.flush_all(self) end
7-
resty_lrucache._VERSION="0.09"
8-
function resty_lrucache.get(self, key) end
9-
return resty_lrucache
2+
3+
---@class resty.lrucache : table
4+
local lrucache = {
5+
_VERSION = "0.11",
6+
}
7+
8+
--- User flags value associated with the item to be stored.
9+
---
10+
--- It can be retrieved later with the item. The user flags are stored as an
11+
--- unsigned 32-bit integer internally, and thus must be specified as a Lua
12+
--- number. If not specified, flags will have a default value of 0. This
13+
--- argument was added in the v0.10 release.
14+
---
15+
---@alias resty.lrucache.flags integer
16+
17+
--- Creates a new cache instance.
18+
---
19+
--- Upon failure, returns nil and a string describing the error.
20+
---
21+
---@param max_items number specifies the maximal number of items this cache can hold.
22+
---@return resty.lrucache? cache
23+
---@return string? error
24+
function lrucache.new(max_items) end
25+
26+
27+
--- Sets a key with a value and an expiration time.
28+
---
29+
--- When the cache is full, the cache will automatically evict the least
30+
--- recently used item.
31+
---
32+
---
33+
---@param key string
34+
---@param value any
35+
---@param ttl? number Expiration time, in seconds. If omitted, the value never expires.
36+
---@param flags? resty.lrucache.flags
37+
function lrucache:set(key, value, ttl, flags) end
38+
39+
40+
--- Fetches a value with the key.
41+
---
42+
--- If the key does not exist in the cache or has already expired, `nil` will
43+
--- be returned.
44+
---
45+
--- Starting from v0.03, the stale data is also returned as the second return
46+
--- value if available.
47+
---
48+
---@param key string
49+
---@return any? data
50+
---@return any? stale_data
51+
---@return resty.lrucache.flags? integer
52+
function lrucache:get(key) end
53+
54+
55+
--- Removes an item specified by the key from the cache.
56+
---
57+
---@param key string
58+
function lrucache:delete(key) end
59+
60+
61+
--- Returns the number of items currently stored in the cache, including expired
62+
--- items if any.
63+
---
64+
--- The returned count value will always be greater or equal to 0 and smaller
65+
--- than or equal to the size argument given to cache:new.
66+
---
67+
--- This method was added in the v0.10 release.
68+
---
69+
---@return integer
70+
function lrucache:count() end
71+
72+
73+
--- Returns the maximum number of items the cache can hold.
74+
---
75+
--- The return value is the same as the size argument given to
76+
--- `resty.lrucache.new()` when the cache was created.
77+
---
78+
--- This method was added in the v0.10 release.
79+
---
80+
---@return integer
81+
function lrucache:capacity() end
82+
83+
84+
--- Fetch the list of keys currently inside the cache, up to `max_count`.
85+
---
86+
--- The keys will be ordered in MRU fashion (Most-Recently-Used keys first).
87+
---
88+
--- This function returns a Lua (array) table (with integer keys) containing
89+
--- the keys.
90+
---
91+
--- When `max_count` is `nil` or `0`, all keys (if any) will be returned.
92+
---
93+
--- When provided with a `res` table argument, this function will not allocate a
94+
--- table and will instead insert the keys in `res`, along with a trailing `nil`
95+
--- value.
96+
---
97+
--- This method was added in the v0.10 release.
98+
---
99+
---@param max_count? integer
100+
---@param res? table
101+
---@return table keys
102+
function lrucache:get_keys(max_count, res) end
103+
104+
105+
--- Flushes all the existing data (if any) in the current cache instance.
106+
---
107+
--- This is an O(1) operation and should be much faster than creating a brand
108+
--- new cache instance.
109+
---
110+
--- Note however that the `flush_all()` method of `resty.lrucache.pureffi` is any
111+
--- O(n) operation.
112+
function lrucache:flush_all() end
113+
114+
115+
return lrucache
Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
---@meta
2-
resty_lrucache_pureffi={}
3-
function resty_lrucache_pureffi.delete(self, key) end
4-
function resty_lrucache_pureffi.new(size, load_factor) end
5-
function resty_lrucache_pureffi.set(self, key, value, ttl) end
6-
function resty_lrucache_pureffi.flush_all(self) end
7-
resty_lrucache_pureffi._VERSION="0.09"
8-
function resty_lrucache_pureffi.get(self, key) end
9-
return resty_lrucache_pureffi
2+
3+
---@class resty.lrucache.pureffi : resty.lrucache
4+
local lrucache_pureffi = {
5+
_VERSION = "0.11",
6+
}
7+
8+
--- Creates a new cache instance.
9+
---
10+
--- Upon failure, returns nil and a string describing the error.
11+
---
12+
---@param max_items number specifies the maximal number of items this cache can hold.
13+
---@param load_factor? number designates the "load factor" of the FFI-based hash-table used internally by `resty.lrucache.pureffi`; the default value is 0.5 (i.e. 50%); if the load factor is specified, it will be clamped to the range of [0.1, 1] (i.e. if load factor is greater than 1, it will be saturated to 1; likewise, if load-factor is smaller than 0.1, it will be clamped to 0.1).
14+
---@return resty.lrucache.pureffi? cache
15+
---@return string? error
16+
function lrucache_pureffi.new(max_items, load_factor) end
17+
18+
return lrucache_pureffi

0 commit comments

Comments
 (0)