> For the complete documentation index, see [llms.txt](https://gotolabs.gitbook.io/lightninguservault/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gotolabs.gitbook.io/lightninguservault/architecture/caching-mechanism.md).

# Caching Mechanism:

At LightningUserVault, we've embraced modularity in caching just as we did with storage.  Simply align with our Cache interface, and you're free to integrate any caching system you fancy. While we're currently powered by `memcache`, the choice is all yours!&#x20;

✅ **Currently supported:**

1. memcache

#### 🌪️ **How It Works**

Our caching mechanism, often referred to as **"Read-Through Cache"**, optimizes data access in the following manner:

1. **First-time Read**: When data is read for the first time, it's fetched from the primary storage and simultaneously stored in the cache.
2. **Subsequent Reads**: For any subsequent requests for the same data, it's swiftly fetched from the cache, ensuring lightning-fast access.

#### 🚀 Future Enhancements:

We aim to expand our caching strategies to include:

* Write-Through Cache
* Write-Around Cache
* Write-Back Cache
* and more.

🔌 **Caching Interface:**

If you're interested in adding more storage options, here's the interface to guide you:

```go
type Cache interface {
	// Set stores a value in the cache with a given key.
	Set(key int64, value *common.User) error

	// Get retrieves a value from the cache using a given key.
	Get(key int64) (*common.User, error)
}
```

#### 🧪 **Mock Implementations for Testing**

For developers and testers, we offer mock implementation of the cache interface

```go
package mock

import "github.com/bradfitz/gomemcache/memcache"

type (
	SetDelegate func(item *memcache.Item) error
	GetDelegate func(key string) (item *memcache.Item, err error)
)

type MockClient struct {
	SetFn SetDelegate
	GetFn GetDelegate
}

func (m *MockClient) Set(item *memcache.Item) error {
	if m.SetFn != nil {
		return m.SetFn(item)
	}

	return nil
}

func (m *MockClient) Get(key string) (item *memcache.Item, err error) {
	if m.GetFn != nil {
		return m.GetFn(key)
	}

	return nil, nil
}
```

**Usage example:**

```go
mockCache := &cacheMock.MockCache{
    GetFn: func(key int64) (*common.User, error) {
        return nil, errUserNotInCache
    },
    SetFn: func(key int64, value *common.User) error {
        return nil
    },
}

```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://gotolabs.gitbook.io/lightninguservault/architecture/caching-mechanism.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.
