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!

โœ… 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:

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

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:

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

Last updated