@quenty/lrucache 1.2.0-canary.490.601c967.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1 -1
- package/package.json +3 -3
- package/src/Shared/LRUCache.lua +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
-
# [1.2.0
|
|
6
|
+
# [1.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/lrucache@1.1.0...@quenty/lrucache@1.2.0) (2024-09-12)
|
|
7
7
|
|
|
8
8
|
**Note:** Version bump only for package @quenty/lrucache
|
|
9
9
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/lrucache",
|
|
3
|
-
"version": "1.2.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "LRUCache library",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/loader": "10.4.0
|
|
28
|
+
"@quenty/loader": "^10.4.0"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "fb172906f3ee725269ec1e5f4daf9dca227e729d"
|
|
34
34
|
}
|
package/src/Shared/LRUCache.lua
CHANGED
|
@@ -28,13 +28,13 @@ SOFTWARE.
|
|
|
28
28
|
local LRUCache = {}
|
|
29
29
|
LRUCache.ClassName = "LRUCache"
|
|
30
30
|
|
|
31
|
-
function LRUCache.new(
|
|
32
|
-
assert(
|
|
33
|
-
assert(not
|
|
31
|
+
function LRUCache.new(maxSize, maxBytes)
|
|
32
|
+
assert(maxSize >= 1, "maxSize must be >= 1")
|
|
33
|
+
assert(not maxBytes or maxBytes >= 1, "maxBytes must be >= 1")
|
|
34
34
|
|
|
35
35
|
-- current size
|
|
36
36
|
local size = 0
|
|
37
|
-
local
|
|
37
|
+
local bytesUsed = 0
|
|
38
38
|
|
|
39
39
|
-- map is a hash map from keys to tuples
|
|
40
40
|
-- tuple: value, prev, next, key
|
|
@@ -94,15 +94,15 @@ function LRUCache.new(max_size, max_bytes)
|
|
|
94
94
|
map[key] = nil
|
|
95
95
|
cut(tuple)
|
|
96
96
|
size = size - 1
|
|
97
|
-
|
|
97
|
+
bytesUsed = bytesUsed - (tuple[BYTES] or 0)
|
|
98
98
|
removed_tuple = tuple
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
-- removes elemenets to provide enough memory
|
|
102
102
|
-- returns last removed element or nil
|
|
103
103
|
local function makeFreeSpace(bytes)
|
|
104
|
-
while size + 1 >
|
|
105
|
-
(
|
|
104
|
+
while size + 1 > maxSize or
|
|
105
|
+
(maxBytes and bytesUsed + bytes > maxBytes)
|
|
106
106
|
do
|
|
107
107
|
assert(oldest, "not enough storage for cache")
|
|
108
108
|
del(oldest[KEY], oldest)
|
|
@@ -126,15 +126,15 @@ function LRUCache.new(max_size, max_bytes)
|
|
|
126
126
|
end
|
|
127
127
|
if value ~= nil then
|
|
128
128
|
-- the value is not removed
|
|
129
|
-
bytes =
|
|
129
|
+
bytes = maxBytes and (bytes or #value) or 0
|
|
130
130
|
makeFreeSpace(bytes)
|
|
131
131
|
local tuple1 = removed_tuple or {}
|
|
132
132
|
map[key] = tuple1
|
|
133
133
|
tuple1[VALUE] = value
|
|
134
134
|
tuple1[KEY] = key
|
|
135
|
-
tuple1[BYTES] =
|
|
135
|
+
tuple1[BYTES] = maxBytes and bytes
|
|
136
136
|
size = size + 1
|
|
137
|
-
|
|
137
|
+
bytesUsed = bytesUsed + bytes
|
|
138
138
|
setNewest(tuple1)
|
|
139
139
|
else
|
|
140
140
|
assert(key ~= nil, "Key may not be nil")
|