@quenty/lrucache 1.0.1-canary.468.ea8a226.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 ADDED
@@ -0,0 +1,11 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ ## 1.0.1-canary.468.ea8a226.0 (2024-05-09)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add LRUCache package ([f5ded94](https://github.com/Quenty/NevermoreEngine/commit/f5ded9480dc991105dfdb3c36d9152329a18882a))
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014-2024 James Onnen (Quenty)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ ## Lrucache
2
+
3
+ <div align="center">
4
+ <a href="http://quenty.github.io/NevermoreEngine/">
5
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/docs.yml/badge.svg" alt="Documentation status" />
6
+ </a>
7
+ <a href="https://discord.gg/mhtGUS8">
8
+ <img src="https://img.shields.io/discord/385151591524597761?color=5865F2&label=discord&logo=discord&logoColor=white" alt="Discord" />
9
+ </a>
10
+ <a href="https://github.com/Quenty/NevermoreEngine/actions">
11
+ <img src="https://github.com/Quenty/NevermoreEngine/actions/workflows/build.yml/badge.svg" alt="Build and release status" />
12
+ </a>
13
+ </div>
14
+
15
+ LRUCache library
16
+
17
+ <div align="center"><a href="https://quenty.github.io/NevermoreEngine/api/LrucacheUtils">View docs →</a></div>
18
+
19
+ ## Installation
20
+
21
+ ```
22
+ npm install @quenty/lrucache --save
23
+ ```
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "lrucache",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": "src"
6
+ }
7
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@quenty/lrucache",
3
+ "version": "1.0.1-canary.468.ea8a226.0",
4
+ "description": "LRUCache library",
5
+ "keywords": [
6
+ "Roblox",
7
+ "Nevermore",
8
+ "Lua",
9
+ "lrucache"
10
+ ],
11
+ "bugs": {
12
+ "url": "https://github.com/Quenty/NevermoreEngine/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "https://github.com/Quenty/NevermoreEngine.git",
17
+ "directory": "src/lrucache/"
18
+ },
19
+ "funding": {
20
+ "type": "patreon",
21
+ "url": "https://www.patreon.com/quenty"
22
+ },
23
+ "license": "MIT",
24
+ "contributors": [
25
+ "Quenty"
26
+ ],
27
+ "dependencies": {
28
+ "@quenty/loader": "10.2.1-canary.468.ea8a226.0"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "gitHead": "ea8a2260a364aaf6d6c246a93632b789935da134"
34
+ }
@@ -0,0 +1,181 @@
1
+ -- lua-lru, LRU cache in Lua
2
+ -- Copyright (c) 2015 Boris Nagaev
3
+ --[[
4
+
5
+ The MIT License (MIT)
6
+
7
+ Copyright (c) 2015 Boris Nagaev
8
+
9
+ Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ of this software and associated documentation files (the "Software"), to deal
11
+ in the Software without restriction, including without limitation the rights
12
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ copies of the Software, and to permit persons to whom the Software is
14
+ furnished to do so, subject to the following conditions:
15
+
16
+ The above copyright notice and this permission notice shall be included in all
17
+ copies or substantial portions of the Software.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+ ]]
27
+
28
+ local LRUCache = {}
29
+ LRUCache.ClassName = "LRUCache"
30
+
31
+ function LRUCache.new(max_size, max_bytes)
32
+ assert(max_size >= 1, "max_size must be >= 1")
33
+ assert(not max_bytes or max_bytes >= 1, "max_bytes must be >= 1")
34
+
35
+ -- current size
36
+ local size = 0
37
+ local bytes_used = 0
38
+
39
+ -- map is a hash map from keys to tuples
40
+ -- tuple: value, prev, next, key
41
+ -- prev and next are pointers to tuples
42
+ local map = {}
43
+
44
+ -- indices of tuple
45
+ local VALUE = 1
46
+ local PREV = 2
47
+ local NEXT = 3
48
+ local KEY = 4
49
+ local BYTES = 5
50
+
51
+ -- newest and oldest are ends of double-linked list
52
+ local newest = nil -- first
53
+ local oldest = nil -- last
54
+
55
+ local removed_tuple -- created in del(), removed in set()
56
+
57
+ -- remove a tuple from linked list
58
+ local function cut(tuple)
59
+ local tuple_prev = tuple[PREV]
60
+ local tuple_next = tuple[NEXT]
61
+ tuple[PREV] = nil
62
+ tuple[NEXT] = nil
63
+ if tuple_prev and tuple_next then
64
+ tuple_prev[NEXT] = tuple_next
65
+ tuple_next[PREV] = tuple_prev
66
+ elseif tuple_prev then
67
+ -- tuple is the oldest element
68
+ tuple_prev[NEXT] = nil
69
+ oldest = tuple_prev
70
+ elseif tuple_next then
71
+ -- tuple is the newest element
72
+ tuple_next[PREV] = nil
73
+ newest = tuple_next
74
+ else
75
+ -- tuple is the only element
76
+ newest = nil
77
+ oldest = nil
78
+ end
79
+ end
80
+
81
+ -- insert a tuple to the newest end
82
+ local function setNewest(tuple)
83
+ if not newest then
84
+ newest = tuple
85
+ oldest = tuple
86
+ else
87
+ tuple[NEXT] = newest
88
+ newest[PREV] = tuple
89
+ newest = tuple
90
+ end
91
+ end
92
+
93
+ local function del(key, tuple)
94
+ map[key] = nil
95
+ cut(tuple)
96
+ size = size - 1
97
+ bytes_used = bytes_used - (tuple[BYTES] or 0)
98
+ removed_tuple = tuple
99
+ end
100
+
101
+ -- removes elemenets to provide enough memory
102
+ -- returns last removed element or nil
103
+ local function makeFreeSpace(bytes)
104
+ while size + 1 > max_size or
105
+ (max_bytes and bytes_used + bytes > max_bytes)
106
+ do
107
+ assert(oldest, "not enough storage for cache")
108
+ del(oldest[KEY], oldest)
109
+ end
110
+ end
111
+
112
+ local function get(_, key)
113
+ local tuple = map[key]
114
+ if not tuple then
115
+ return nil
116
+ end
117
+ cut(tuple)
118
+ setNewest(tuple)
119
+ return tuple[VALUE]
120
+ end
121
+
122
+ local function set(_, key, value, bytes)
123
+ local tuple = map[key]
124
+ if tuple then
125
+ del(key, tuple)
126
+ end
127
+ if value ~= nil then
128
+ -- the value is not removed
129
+ bytes = max_bytes and (bytes or #value) or 0
130
+ makeFreeSpace(bytes)
131
+ local tuple1 = removed_tuple or {}
132
+ map[key] = tuple1
133
+ tuple1[VALUE] = value
134
+ tuple1[KEY] = key
135
+ tuple1[BYTES] = max_bytes and bytes
136
+ size = size + 1
137
+ bytes_used = bytes_used + bytes
138
+ setNewest(tuple1)
139
+ else
140
+ assert(key ~= nil, "Key may not be nil")
141
+ end
142
+ removed_tuple = nil
143
+ end
144
+
145
+ local function delete(_, key)
146
+ return set(nil, key, nil)
147
+ end
148
+
149
+ local function mynext(_, prev_key)
150
+ local tuple
151
+ if prev_key then
152
+ tuple = map[prev_key][NEXT]
153
+ else
154
+ tuple = newest
155
+ end
156
+ if tuple then
157
+ return tuple[KEY], tuple[VALUE]
158
+ else
159
+ return nil
160
+ end
161
+ end
162
+
163
+ -- returns iterator for keys and values
164
+ local function lru_pairs()
165
+ return mynext, nil, nil
166
+ end
167
+
168
+ local mt = {
169
+ __index = {
170
+ get = get,
171
+ set = set,
172
+ delete = delete,
173
+ pairs = lru_pairs,
174
+ },
175
+ __pairs = lru_pairs,
176
+ }
177
+
178
+ return setmetatable({}, mt)
179
+ end
180
+
181
+ return LRUCache
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "node_modules",
3
+ "globIgnorePaths": [ "**/.package-lock.json" ],
4
+ "tree": {
5
+ "$path": { "optional": "../node_modules" }
6
+ }
7
+ }
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "LrucacheTest",
3
+ "tree": {
4
+ "$className": "DataModel",
5
+ "ServerScriptService": {
6
+ "lrucache": {
7
+ "$path": ".."
8
+ }
9
+ }
10
+ }
11
+ }