github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/doc/content/en/docs/javascript/storage.md (about)

     1  
     2  ---
     3  title: "Storage"
     4  linkTitle: "storage"
     5  date: 2021-11-19
     6  description: >
     7  
     8  ---
     9  
    10  In the **Smart Home** project, there is an `in-memory` storage provided, which allows you to store and cache arbitrary values in memory. The values are also periodically archived to disk every minute.
    11  
    12  {{< alert color="success" >}}This function is available in any system script.{{< /alert >}}
    13  
    14  The following methods are available through the "Storage" object for working with the storage:
    15  
    16  1. `push(key, value)`: This method is used to add a value to the storage. You pass the `key` and `value` as arguments, which will be stored in memory. Example usage:
    17  
    18  ```javascript
    19  Storage.push('temperature', 25.5);
    20  ```
    21  
    22  2. `getByName(key)`: This method allows you to retrieve a value from the storage based on the specified `key`. If the value is found in memory, it will be returned. Example usage:
    23  
    24  ```javascript
    25  const temperature = Storage.getByName('temperature');
    26  console.log(temperature);
    27  ```
    28  
    29  3. `search(key)`: This method performs a search for a value based on the `key`. It first searches for the value in memory, and if not found, it searches in the database. The result of the search is returned. Example usage:
    30  
    31  ```javascript
    32  const result = Storage.search('temperature');
    33  console.log(result);
    34  ```
    35  
    36  4. `pop(key)`: This method removes a value from the storage based on the specified `key`. If the value is found and removed, the method returns the removed value. Example usage:
    37  
    38  ```javascript
    39  const removedValue = Storage.pop('temperature');
    40  console.log(removedValue);
    41  ```
    42  
    43  The methods of the "Storage" object provide the ability to add, retrieve, search, and remove values from the `in-memory` storage. You can use this storage for temporary data storage in memory, with the option to save to disk and perform efficient value search.
    44  
    45  ----------------
    46  
    47  ### Working with the storage
    48  ```coffeescript
    49  Storage
    50    .push(key, value)
    51    .getByName(key)
    52    .search(key)
    53    .pop(key)
    54  ```
    55  
    56  |  Value  | Description  |
    57  |-------------|---------|
    58  | push |    Store a value in the storage with the specified `key`  |
    59  | getByName | Retrieve a value from the storage based on the specified `key` |
    60  | search | Search for a value based on the specified `key` |
    61  | pop | Remove a record |
    62  
    63  ----------------
    64  
    65  ### Code example
    66  
    67  ```coffeescript
    68  # Storage
    69  # ##################################
    70  
    71  foo =
    72    'bar': 'bar'
    73  
    74  value = JSON.stringify foo
    75  
    76  # save var
    77  Storage.push 'foo', value
    78  
    79  # get exist var
    80  value = Storage.getByName 'foo'
    81  
    82  # search
    83  list = Storage.search 'bar'
    84  
    85  Storage.pop 'foo'
    86  ```