$wsStore module is a key-value store for the frontend. Through it, arbitrary data can be stored under a freely chosen key, read out again later, or deleted in a targeted way.
It is the right tool wherever you need information that the shop system does not provide by default.
Module overview
Example / excerpt of$wsStore
"ƒ()" denotes a function.
Methods overview
| Name | Return type | Description |
|---|---|---|
set() | - | Stores a value under the specified key. |
get() | any / null | Returns the stored value for the specified key. |
delete() | - | Deletes the entry for the specified key. |
increment() | - | Increments a counter value under the specified key. |
Methods
$wsStore.set()
Stores an arbitrary value under a key. The value can later be retrieved via get() . Optionally, a validity duration (ttl) can be specified in seconds, after which the entry is automatically removed. Without ttl, a default value of 365 days applies.
Signature$wsStore.set(key, value, ttl)
Return value
Parameters| Name | Type | Required | Description |
|---|---|---|---|
key | string | yes | The key under which the value is stored. Freely chosen. Recommendation: use meaningful, unique names with a prefix (e.g. 'productViews-' + $cProduct.id) to rule out collisionswith other modules. |
value | any | yes | The value to be stored. (integer, string, boolean, object, array) |
ttl | int | no | Validity duration in seconds. After expiration, the entry is automatically removed. Without specification, a default value of 365 days applies. |
14400 seconds equal 4 hours – the marker is valid for this long.
From the time of setting,
$wsStore.get('productViewSession-<ProductID><SessionID>') returns the value true for 4 hours. The actual counter that outputs the display “X times viewed in the last 24 hours” to the visitor is maintained via increment(). Only after the 4 hours have elapsed may the same visitor’s product view flow into the counter again.
$wsStore.get()
Returns the value that was previously stored with set() under the specified key. If no entry exists or the entry has expired,null is returned.
Signature$wsStore.get(key)
Return valueany - The stored value, or null, if no entry exists or the entry has expired.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
key | string | yes | The key whose value is to be retrieved. |
$wsStore.delete()
Deletes the entry for the specified key from the store. After deletion, get() returnsnull for this key.
Signature$wsStore.delete(key)
Return value\
Parameters| Name | Type | Required | Description |
|---|---|---|---|
key | string | yes | The key whose entry is to be deleted. |
$wsStore.increment()
Increments a counter value under the specified key by the specified amount. If the key does not yet exist, it is created with the value ofamount.
This method should be preferred over a combination of get() and set() when a counter is to be incremented.This is because get() and set() perform the increment in two steps - first read the current value, then store the incremented value. If two visitors open the page at the same time, both read the same value in step 1, for example 5. Both then store 6. The counter ends up at 6 even though it should be at 7. With
increment(), reading and incrementing happen in a single step, so both calls are counted correctly.
Signature$wsStore.increment(key, amount, ttl)
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
key | string | yes | The key whose counter value is to be incremented. |
amount | int | no | The value by which the counter is incremented. Default is 1. |
ttl | int | no | Validity duration in seconds. After expiration, the entry is automatically removed. If ttl is omitted, a default value of 365 days applies. |
The counter
productViews-<ProductID> is incremented at most once every 4 hours per visitor. The subsequent display with get() shows the visitor for example: “7 times viewed in the last 24 hours”. After 24 hours, the counter automatically restarts at zero.