# Cache

The `Xmf\Module\Helper\Cache` class is a module aware helper for using the system cache for module related data. The underlying cache is a key value store. This helper isolates the module related data by applying a module specific prefix to each key.

`Xmf\Module\Helper\Cache` extends [Xmf\Module\Helper\AbstractHelper](https://github.com/XoopsDocs/xmf-cookbook/tree/fe9f7aa71cb38a3faba2ff745352b2de77a33b6f/en/book/module/abstracthelper.php).

## new Cache(*$dirname*)

Creates the cache helper for the module specified by name as *$dirname*. If the string *$dirname* is empty, the current module in XOOPS will be used.

## write(*$key*, *$value*, *$ttl*)

Write the value *$value* for a key named *$key* to the cache, using the specified number of seconds in *$ttl* as the time to live.

## read(*$key*, *$default*)

Read value for the key named *$key* from the cache. If the key does not exist, it returns *$default*, or *false* if *$default* was not specified.

## delete(*$key*)

Deletes any key named *$key* from the cache.

## cacheRead(*$key*, *$regenFunction*, *$ttl*, *$args*)

The cacheRead() method combines reading and any needed regenerating of the cache entry into a single call.

First, it attempts to read the cache entry for *$key*, and returns it if found.

If the cache read fails, it calls the specified callable, *$regenFunction*, passing to it any variable arguments, *$args*. It writes the return of *$regenFunction* to the cache for *$key* with time to live *$ttl*, and returns the new cached value to the caller.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xoops.gitbook.io/xmf-cookbook/reference/module/helper-1/cache.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
