@next2d/cache 3.8.1 → 3.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@next2d/cache",
3
- "version": "3.8.1",
3
+ "version": "3.9.0",
4
4
  "description": "Next2D Cache Package",
5
5
  "author": "Toshiyuki Ienaga<ienaga@next2d.app> (https://github.com/ienaga/)",
6
6
  "license": "MIT",
@@ -22,5 +22,6 @@
22
22
  "repository": {
23
23
  "type": "git",
24
24
  "url": "git+https://github.com/Next2D/Player.git"
25
- }
25
+ },
26
+ "sideEffects": false
26
27
  }
@@ -1,3 +1,4 @@
1
+ import type { CacheStore } from "../../CacheStore";
1
2
  /**
2
3
  * @description 指定のキーからデータを取得
3
4
  * Get data from the specified key
@@ -18,4 +19,4 @@
18
19
  * @method
19
20
  * @public
20
21
  */
21
- export declare const execute: (data_store: Map<string, Map<string, any>>, trash_store: Map<string, Map<string, any>>, unique_key: string, key: string) => any;
22
+ export declare const execute: (cache_store: CacheStore, data_store: Map<string, Map<string, any>>, trash_store: Map<string, Map<string, any>>, unique_key: string, key: string) => any;
@@ -18,7 +18,7 @@
18
18
  * @method
19
19
  * @public
20
20
  */
21
- export const execute = (data_store, trash_store, unique_key, key) => {
21
+ export const execute = (cache_store, data_store, trash_store, unique_key, key) => {
22
22
  const data = data_store.get(unique_key) || null;
23
23
  if (!data) {
24
24
  return null;
@@ -27,5 +27,10 @@ export const execute = (data_store, trash_store, unique_key, key) => {
27
27
  trash_store.delete(unique_key);
28
28
  data.delete("trash");
29
29
  }
30
+ // LRU: 上限が設定されている場合、参照したIDを最新に更新する
31
+ if (cache_store.$maxStoreSize > 0) {
32
+ data_store.delete(unique_key);
33
+ data_store.set(unique_key, data);
34
+ }
30
35
  return data.get(key) || null;
31
36
  };
@@ -31,4 +31,21 @@ export const execute = (cache_store, data_store, unique_key, key, value = null)
31
31
  }
32
32
  // set cache
33
33
  data.set(key, value);
34
+ // LRU: 上限が設定されている場合(メインスレッドのopt-in)、
35
+ // 触れたIDを最新に更新し、超過分を古い順にevictする。
36
+ // evictは既存のタイマー削除と同じ経路(removeById + $removeIds)で
37
+ // worker側のGPUリソースも解放される。上限0(デフォルト)では従来挙動のまま。
38
+ if (cache_store.$maxStoreSize > 0) {
39
+ data_store.delete(unique_key);
40
+ data_store.set(unique_key, data);
41
+ while (data_store.size > cache_store.$maxStoreSize) {
42
+ const oldestId = data_store.keys().next().value;
43
+ // 触れたばかりのIDは削除しない(上限1未満の設定に対する安全弁)
44
+ if (oldestId === unique_key) {
45
+ break;
46
+ }
47
+ cache_store.removeById(oldestId);
48
+ cache_store.$removeIds.push(+oldestId);
49
+ }
50
+ }
34
51
  };
@@ -55,6 +55,20 @@ export declare class CacheStore {
55
55
  * @public
56
56
  */
57
57
  readonly $removeIds: number[];
58
+ /**
59
+ * @description キャッシュストアの最大エントリ数(LRU)。0は無制限(デフォルト・従来挙動)。
60
+ * 上限はメインスレッド側でのみ設定し、evictしたIDは$removeIds経由で
61
+ * worker側のGPUリソース(アトラスノード等)も解放される。
62
+ * Maximum number of cache entries (LRU). 0 means unlimited
63
+ * (default, legacy behavior). Set only on the main thread;
64
+ * evicted IDs are propagated to the worker via $removeIds so
65
+ * GPU resources (atlas nodes etc.) are released as well.
66
+ *
67
+ * @type {number}
68
+ * @default 0
69
+ * @public
70
+ */
71
+ $maxStoreSize: number;
58
72
  /**
59
73
  * @constructor
60
74
  */
package/src/CacheStore.js CHANGED
@@ -101,12 +101,32 @@ export class CacheStore {
101
101
  writable: true,
102
102
  value: void 0
103
103
  });
104
+ /**
105
+ * @description キャッシュストアの最大エントリ数(LRU)。0は無制限(デフォルト・従来挙動)。
106
+ * 上限はメインスレッド側でのみ設定し、evictしたIDは$removeIds経由で
107
+ * worker側のGPUリソース(アトラスノード等)も解放される。
108
+ * Maximum number of cache entries (LRU). 0 means unlimited
109
+ * (default, legacy behavior). Set only on the main thread;
110
+ * evicted IDs are propagated to the worker via $removeIds so
111
+ * GPU resources (atlas nodes etc.) are released as well.
112
+ *
113
+ * @type {number}
114
+ * @default 0
115
+ * @public
116
+ */
117
+ Object.defineProperty(this, "$maxStoreSize", {
118
+ enumerable: true,
119
+ configurable: true,
120
+ writable: true,
121
+ value: void 0
122
+ });
104
123
  this._$pool = [];
105
124
  this._$store = new Map();
106
125
  this._$trash = new Map();
107
126
  this.$timerId = null;
108
127
  this.$removeIds = [];
109
128
  this.$removeCache = false;
129
+ this.$maxStoreSize = 0;
110
130
  }
111
131
  /**
112
132
  * @description 登録された全てのキャッシュをリセット・破棄する
@@ -212,7 +232,13 @@ export class CacheStore {
212
232
  * @public
213
233
  */
214
234
  getById(id) {
215
- return this._$store.get(id);
235
+ const data = this._$store.get(id);
236
+ // LRU: 上限が設定されている場合、参照したIDを最新に更新する
237
+ if (data && this.$maxStoreSize > 0) {
238
+ this._$store.delete(id);
239
+ this._$store.set(id, data);
240
+ }
241
+ return data;
216
242
  }
217
243
  /**
218
244
  * @description 指定のキーのキャッシュデータを返却
@@ -225,7 +251,7 @@ export class CacheStore {
225
251
  * @public
226
252
  */
227
253
  get(unique_key, key) {
228
- return cacheStoreGetService(this._$store, this._$trash, unique_key, key);
254
+ return cacheStoreGetService(this, this._$store, this._$trash, unique_key, key);
229
255
  }
230
256
  /**
231
257
  * @description キャッシュストアにデータをセット