cstorage

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: MIT Imports: 2 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

CStorage

Simple Go cache storage package. It provides key-value storage with TTL(time to live), and Eviction Policy.

Documentation

Overview

CStorage package is module for provide key - value cache storage Outsiders can use following; Get, Put, Delete, Clear, which are self explanatory

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CStorage

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

CStorage structure is struct for holding data structure and misc of cache storage. CStorage uses hash table, and doubly linked list for eviction policy. - Hash Table: since CStorage is key-value store, hash table should be good choice since it has O(logN) to insert and search. - Double Linked List: length of data would be limited, and eviction will be happen in LRU manner(Least Recently Used). To implement this, I will use double linked list here.

func New

func New(config CStorageConfig) *CStorage

New function is initializer of CStorage. It takes CStorageConfig as parameter, which acts as configuration, and returns the pointer to CStorage.

func (*CStorage) Cap

func (s *CStorage) Cap() (capacity int64)

Cap function will return maximum size(capacity) of CStorage.

func (*CStorage) Clear

func (s *CStorage) Clear()

Clear function is to clear all data from CStroage.

func (*CStorage) Delete

func (s *CStorage) Delete(key string) (hit bool)

Delete function is to manually deletes key-value from CStorage.

func (*CStorage) Get

func (s *CStorage) Get(key string) (data []byte, hit bool)

Get function is to get data with key in cache storage. Since CStorage is key-value store, data can be found by key. If Get function is called, following would be happen - Search hashmap - If there is no data with key, it will return empty data with hit=false - If ttl is expired, it will delete record and return hit=false - If none of above, it will move the node by eviction policy, and return data with hit=true

func (*CStorage) Put

func (s *CStorage) Put(key string, data []byte) (hit bool)

Put function is to upsert data with key in cache storage. It will return hit=true if it is update or hit=false if the key didn't existed before. If Put function is called following will happen - Search hashmap with provided key - If key is there, just update data, renew ttl, move node according to eviction policy, return hit=true - If key is not there, next will happen - Check storage size is full, if full, remove one node in accordance to eviction policy - Push key-data to hashmap, place it with eviction policy, return hit=false *Note that hit is just key hits. Not the operation is successful or not.

func (*CStorage) RemoveExpired

func (s *CStorage) RemoveExpired() int64

RemoveExpired function will traverse CStorage and will remove all expired key. Since current version of CStorage uses passive method for ttl, it is possible for CStorage to hold already expired key. This function should be called in regular basis to avoid memory efficiency

func (*CStorage) Size

func (s *CStorage) Size() (size int64)

Size function will return current size of CStorage *Note that in this version, CStorage will hold expired key since ttl deletion will passively happens

type CStorageConfig

type CStorageConfig struct {
	Ttl      time.Duration
	Capacity int64
}

CStorageConfig structure should be provided when outside code calls New() function. It will set properties of storage such as ttl or capacity

Jump to

Keyboard shortcuts

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