memory_ttl_storage

package module
v0.0.0-...-faaba3a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2021 License: GPL-3.0 Imports: 10 Imported by: 0

README

memory_ttl_storage

A Personal implementation of memcached. Allow backup/restore on init, finish and on process.

You can store any struct with a TTL and retrieve by key.

Current Functions

func Add(key string, content interface{}, ttl *int64)
func Get(key string) (interface{}, bool)
func GetAndRefresh(key string) (interface{}, bool)
func Delete(key string) 
func RegisterInterface(i interface{})
// Stop ticker and Store data if UseBackup
func Stop()

Basic Usage

Import the package

import "github.com/hpardora/memory_ttl_storage"

To work with default values

type Example struct {}

mts := New(nil)
mts.Add("key",&Example{}, nil)

item, ok := mts.Get("key")
if !ok {
    t.Error("cannot retrieve from service")
}

restored := item.(*Example)

Start with custom config

cfg := &MemoryTTLStoreConfig{
	TickerTime: (*time.Duration)  // How often the ticker ticks
	TTLValue:   (int64)            // Default number of seconds for items TTL 
	ShowLogs:   (bool)             // if you what to show basic logs...
	UseBackup:  (bool)             // true for save cache data on Stop(), on process and restore on startup
	BackupPath: (string)           // by default /opt/memory_ttl_storage/mtstorage.dat
}
mts := New(cfg)

If you want to store custom structs, you must add the following code: mts.RegisterInterface(YourStruct{})

Update the DefaultTTL

mts := New(nil)
mts.SetDefaultTTL(100)

TODO

  • Add a size limit

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	Content         interface{}
	ExpireTimestamp int64
	TTL             int64
}

type MemoryTTLStorage

type MemoryTTLStorage struct {
	// contains filtered or unexported fields
}

func (*MemoryTTLStorage) Add

func (mts *MemoryTTLStorage) Add(key string, content interface{}, ttl *int64)

func (*MemoryTTLStorage) Delete

func (mts *MemoryTTLStorage) Delete(key string)

func (*MemoryTTLStorage) Get

func (mts *MemoryTTLStorage) Get(key string) (interface{}, bool)

func (*MemoryTTLStorage) GetAndRefresh

func (mts *MemoryTTLStorage) GetAndRefresh(key string) (interface{}, bool)

func (*MemoryTTLStorage) NewBackupTicker

func (mts *MemoryTTLStorage) NewBackupTicker() *time.Ticker

func (*MemoryTTLStorage) NewCleanerTicker

func (mts *MemoryTTLStorage) NewCleanerTicker(tickerTime time.Duration) *time.Ticker

func (*MemoryTTLStorage) RegisterInterface

func (mts *MemoryTTLStorage) RegisterInterface(i interface{})

func (*MemoryTTLStorage) SetDefaultTTL

func (mts *MemoryTTLStorage) SetDefaultTTL(defaultTTL int64)

func (*MemoryTTLStorage) Stop

func (mts *MemoryTTLStorage) Stop()

type MemoryTTLStoreConfig

type MemoryTTLStoreConfig struct {
	TickerTime time.Duration
	TTLValue   int64
	ShowLogs   bool
	UseBackup  bool
	BackupPath string
}

type StorageManager

type StorageManager struct {
	// contains filtered or unexported fields
}

func NewStorageManager

func NewStorageManager(backupFilePath string) *StorageManager

func (*StorageManager) Restore

func (b *StorageManager) Restore(i interface{}) error

func (*StorageManager) Store

func (b *StorageManager) Store(i interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL