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 ¶
- type CStorage
- func (s *CStorage) Cap() (capacity int64)
- func (s *CStorage) Clear()
- func (s *CStorage) Delete(key string) (hit bool)
- func (s *CStorage) Get(key string) (data []byte, hit bool)
- func (s *CStorage) Put(key string, data []byte) (hit bool)
- func (s *CStorage) RemoveExpired() int64
- func (s *CStorage) Size() (size int64)
- type CStorageConfig
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) Clear ¶
func (s *CStorage) Clear()
Clear function is to clear all data from CStroage.
func (*CStorage) Get ¶
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 ¶
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 ¶
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
type CStorageConfig ¶
CStorageConfig structure should be provided when outside code calls New() function. It will set properties of storage such as ttl or capacity