Server IP : 80.87.202.40 / Your IP : 216.73.216.169 Web Server : Apache System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64 User : bitrix ( 600) PHP Version : 8.2.27 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /home/bitrix/ext_www/rospirotorg.ru/bitrix/js/location/core/src/common/ |
Upload File : |
import { md5 } from 'main.md5'; const MAX_ITEMS_CNT = 100; const MAX_SIZE_IN_BYTES = 5 * 1024 * 1024; const CACHE_TTL = 3600; export default class AutocompleteCache { static set(sourceCode: string, params: Object, data: Object) { const results = AutocompleteCache.#getAll(sourceCode); results.push({ hash: AutocompleteCache.#makeParamsHash(params), data: data, }); BX.localStorage.set( AutocompleteCache.#getStorageName(sourceCode), AutocompleteCache.#getResultsToStore(results), CACHE_TTL ); } static get(sourceCode: string, params: Object): ?Object { const hash = AutocompleteCache.#makeParamsHash(params); const results = AutocompleteCache.#getAll(sourceCode); for (const result of results) { if (result && result.hash === hash) { return result; } } return null; } static #getResultsToStore(results: Array): Array { if (new Blob([JSON.stringify(results)]).size > MAX_SIZE_IN_BYTES) { return []; } if (results.length > MAX_ITEMS_CNT) { return results.slice(results.length - MAX_ITEMS_CNT); } return results; } static #getAll(sourceCode: string): Array { const currentResults = BX.localStorage.get(AutocompleteCache.#getStorageName(sourceCode)); return Array.isArray(currentResults) ? currentResults : []; } static #makeParamsHash(params: Object): string { return md5(JSON.stringify(params)); } static #getStorageName(sourceCode: string): string { return `location${sourceCode}AutocompleteCache`; } }