cachedrepo

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: MIT Imports: 1 Imported by: 0

README

cached-repository

Go Report Card

a basic repository to support cached data in memory and is based LRU-K cache replacing algorithm

TODOs
  • LRU-1 & LRU-K

  • Cached-Repository demo

  • LRU-K concurrent safe

  • LRU-1 concurrent safe

Quick Start

simple

package main

import (
	"fmt"

	cachedrepo "github.com/yeqown/cached-repository"
	"github.com/yeqown/cached-repository/lru"
)

func main() {
	c, err := lru.NewLRUK(2, 2, 10, nil)
	if err != nil {
		panic(err)
	}
	ca := cachedrepo.New(c)
	v, ok := ca.Get("key1")
	fmt.Printf("ca.Get('key1')=%v,%v\n", v, ok)

	// put key1, twice means 2 times visit
	ca.Put("key1", "value1")
	ca.Put("key1", "value1")
	v, ok = ca.Get("key1")
	fmt.Printf("ca.Get('key1')=%v,%v\n", v, ok)

	// update key1
	ca.Update("key1", "value111")
	v, ok = ca.Get("key1")
	fmt.Printf("ca.Get('key1')=%v,%v\n", v, ok)

	// put key2, key3 twice
	ca.Put("key2", "value2")
	ca.Put("key2", "value2")
	ca.Put("key3", "value3")
	ca.Put("key3", "value3")

	// query key1 again
	v, ok = ca.Get("key1")
	fmt.Printf("ca.Get('key1')=%v,%v\n", v, ok)

	// query key2
	v, ok = ca.Get("key2")
	fmt.Printf("ca.Get('key2')=%v,%v\n", v, ok)

	// query key3
	v, ok = ca.Get("key3")
	fmt.Printf("ca.Get('key3')=%v,%v\n", v, ok)
}
ca.Get('key1')=<nil>,false
ca.Get('key1')=value1,true
ca.Get('key1')=value111,true
ca.Get('key1')=<nil>,false
ca.Get('key2')=value2,true
ca.Get('key3')=value3,true
Examples

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheAlgor

type CacheAlgor interface {
	Put(key, value interface{})
	Get(key interface{}) (value interface{}, ok bool)
	Update(key, value interface{})
	Delete(key interface{})
}

CacheAlgor is an interface implements different alg.

func New

func New(c lru.Cache) CacheAlgor

New .

type LRUCacheAlgor

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

LRUCacheAlgor .

func (LRUCacheAlgor) Delete

func (a LRUCacheAlgor) Delete(key interface{})

Delete of LRUCacheAlgor

func (LRUCacheAlgor) Get

func (a LRUCacheAlgor) Get(key interface{}) (value interface{}, ok bool)

Get of LRUCacheAlgor

func (LRUCacheAlgor) Put

func (a LRUCacheAlgor) Put(key, value interface{})

Put of LRUCacheAlgor

func (LRUCacheAlgor) Update

func (a LRUCacheAlgor) Update(key, value interface{})

Update of LRUCacheAlgor

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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