62 lines
2.5 KiB
Markdown
62 lines
2.5 KiB
Markdown
# DB Memory Cache for PrestaShop
|
|
|
|
A high-performance caching module for PrestaShop 8 and 9 that utilizes the MySQL/MariaDB `MEMORY` (HEAP) engine. This module provides a fast, ephemeral key-value store directly in RAM without requiring external services like Redis or Memcached.
|
|
|
|
## Features
|
|
|
|
* **Speed:** Uses the `MEMORY` database engine to store data in RAM, avoiding disk I/O.
|
|
* **TTL Support:** Simulates Time-To-Live expiration for cached items.
|
|
* **Unicode Optimized:** Uses `JSON_UNESCAPED_UNICODE` to save space and support multi-byte characters efficiently.
|
|
* **Safety:** Automatically rejects data larger than the defined limit (~16KB) to prevent SQL errors or JSON truncation.
|
|
* **Strict Logic:** Ensures clean writes by deleting existing keys before insertion.
|
|
|
|
## Requirements
|
|
|
|
* PrestaShop 8.0.0 or higher (Compatible with PS 9).
|
|
* MySQL or MariaDB.
|
|
* The database user must have `CREATE` and `DROP` privileges (standard for PS).
|
|
|
|
## Installation
|
|
|
|
1. Create a folder named `dbmemorycache` in your PrestaShop `modules/` directory.
|
|
2. Upload `dbmemorycache.php` and `logo.png` (optional) to that folder.
|
|
3. Go to **Back Office > Modules > Module Manager**.
|
|
4. Search for "MySQL Memory Cache Optimized".
|
|
5. Click **Install**.
|
|
|
|
## Technical Limitations
|
|
|
|
* **Volatility:** Data stored in `MEMORY` tables is lost if the MySQL server restarts. Do not use this for persistent data.
|
|
* **Size Limit:** The maximum value size is approximately **16,000 characters** (due to `VARCHAR` limits in MEMORY tables and `utf8mb4` encoding). If you attempt to cache data larger than this, the `set()` method will return `false` and the data will not be cached.
|
|
|
|
## Usage Examples
|
|
|
|
You can call this module from any other module, controller, or hook in PrestaShop.
|
|
|
|
### 1. Basic Usage (Get & Set)
|
|
|
|
```php
|
|
// 1. Get the module instance
|
|
$cache = Module::getInstanceByName('dbmemorycache');
|
|
|
|
// Ensure module is installed and active
|
|
if ($cache && Module::isInstalled('dbmemorycache')) {
|
|
$key = 'my_custom_api_data_v1';
|
|
|
|
// 2. Check if data exists and is valid (not expired)
|
|
if ($cache->existsValue($key)) {
|
|
$data = $cache->getValue($key);
|
|
// $data is now your array/object
|
|
} else {
|
|
// 3. Fetch your expensive data
|
|
$data = [
|
|
'id' => 123,
|
|
'name' => 'Product Name',
|
|
'features' => ['A', 'B', 'C']
|
|
];
|
|
|
|
// 4. Store in cache for 1 hour (3600 seconds)
|
|
// Returns true on success, false if data is too big
|
|
$cache->setValue($key, $data, 3600);
|
|
}
|
|
} |