> ## Documentation Index
> Fetch the complete documentation index at: https://dokumentation.websale.de/llms.txt
> Use this file to discover all available pages before exploring further.

# $wsStore - Data storage

> Reference for $wsStore: a key-value store in the WEBSALE frontend to save, retrieve and delete arbitrary data the shop does not provide by default.

The `$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`

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{= $wsStore | json }}
```

**JSON output**

```json theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{
  "set": "ƒ()",
  "get": "ƒ()",
  "delete": "ƒ()",
  "increment": "ƒ()"
}
```

Note: `"ƒ()"` 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<br />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 [<u>get()</u>](#\$wsstore-get "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.get()")<u> </u>. 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.<br />Freely chosen.<br />Recommendation:<br />use meaningful, unique names with a prefix<br />(e.g. `'productViews-' + $cProduct.id`) to rule out collisions<br />with other modules. |
| `value`  | any      | yes          | The value to be stored. (integer, string, boolean, object,<br />array)                                                                                                                                                         |
| `ttl`    | int      | no           | Validity duration in seconds. After expiration, the entry is <br />automatically removed. Without specification, a default value <br />of 365 days applies.                                                                    |

**Example - Set a session marker so that a product view is counted only once per visitor**

To implement the display "X times viewed in the last 24 hours" on the product page, you must prevent the same visitor from falsifying the counter by repeatedly reloading the page. To do this, a marker is set per visitor – as long as this marker exists, the product view is not counted again for this visitor.

[<u>\$</u>](/en/frontend/referenz/module/wssession#\$wssession-id "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3046572054/wsSession#%24wsSession.id")<u>wsSession.id</u> is the unique session ID of the current visitor. It is automatically provided by the shop system and does not have to be assigned manually. `14400` seconds equal 4 hours – the marker is valid for this long.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ $wsStore.set('productViewSession-' + $cProduct.id + $wsSession.id, true, 14400) }}
```

**Result**\
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 [<u>increment()</u>](#\$wsstore-increment "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.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 [<u>set()</u>](#\$wsstore-set "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%5BhardBreak%5D%24wsStore.set()") under the specified key. If no entry exists or the entry has expired, `null` is returned.

**Signature**\
`$wsStore.get(key)`

**Return value**\
`any` - 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. |

**Example - Display "X times viewed in the last 24 hours" on the product page**

To output to the visitor on a product detail page a display like "X times viewed in the last 24 hours", the value previously incremented with [<u>increment()</u>](#\$wsstore-increment "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.increment()") is read out. If no entry exists yet (because the product has not yet been viewed in the last 24 hours), [get()](#\$wsstore-get) returns null and the display is skipped.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $productViews = $wsStore.get('productViews-' + $cProduct.id) }}
{{ if $productViews }}
    {{= $productViews }} times viewed in the last 24 hours
{{ /if }}
```

**Result, if \$productViews contains the value 10**

```text theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
10 times viewed in the last 24 hours
```

***

### \$wsStore.delete()

Deletes the entry for the specified key from the store. After deletion, [<u>get()</u>](#\$wsstore-get "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.get()") returns `null` 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 of `amount`.

This method should be preferred over a combination of [<u>get()</u>](#\$wsstore-get "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.get()") and [<u>set()</u>](#\$wsstore-set "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%5BhardBreak%5D%24wsStore.set()") when a counter is to be incremented.\
\
This is because [<u>get()</u>](#\$wsstore-get "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.get()") and [<u>set()</u>](#\$wsstore-set "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%5BhardBreak%5D%24wsStore.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. <br />Default is `1`.                                                                                            |
| `ttl`    | int      | no           | Validity duration in seconds. After expiration, <br />the entry is automatically removed. <br />If `ttl` is omitted, a default value <br />of 365 days applies. |

**Example - Count product views, once per session**

The goal is to display "X times viewed in the last 24 hours" on the product page. So that the same visitor does not falsify the counter by repeatedly reloading, a marker is additionally set per session that prevents the same visitor from counting the product multiple times.

[\$wsSession.id](/en/frontend/referenz/module/wssession) is the unique session ID of the current visitor. It is automatically provided by the shop system and does not have to be assigned manually.

```html theme={"theme":{"light":"github-light","dark":"github-dark"},"languages":{"custom":["/languages/websale.json"]}}
{{ var $myViewKey = 'productViews-' + $cProduct.id }}
{{ var $mySessionKey = 'productViewSession-' + $cProduct.id + $wsSession.id }}

{{ if not $wsStore.get($mySessionKey) }}
    {{ $wsStore.increment($myViewKey, 1, 86400) }}
    {{ $wsStore.set($mySessionKey, true, 14400) }}
{{ /if }}
```

**Result**\
The counter `productViews-<ProductID>` is incremented at most once every 4 hours per visitor. The subsequent display with [<u>get()</u>](#\$wsstore-get "https://websale.atlassian.net/wiki/spaces/WSDOKU/pages/3987013651/wsStore#%24wsStore.get()") shows the visitor for example: "7 times viewed in the last 24 hours". After 24 hours, the counter automatically restarts at zero.
