cache

package
v0.6.6 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: MIT Imports: 4 Imported by: 1

README

github.com/gkzy/gow/lib/cache

go mem cache 封装

使用的第三方库
github.com/patrickmn/go-cache
引用
import "github.com/gkzy/gow/lib/cache"
使用

cache_test.go

/*
一个实现的demo
*/

package cache

import (
    "fmt"
    "testing"
)

//================原始数据操作层========================

//prov model
type Prov struct {
    ID   int64
    Name string
}

//from mysql or other...
func (m *Prov) GetAllDataFromDB() (data []*Prov, err error) {
    data = make([]*Prov, 0)
    data = append(data, &Prov{
        ID:   51,
        Name: "四川",
    })
    data = append(data, &Prov{
        ID:   50,
        Name: "重庆",
    })
    data = append(data, &Prov{
        ID:   11,
        Name: "北京",
    })
    return
}

//===========================cache 实现=====================================

// ProvCache 省份信息缓存 一个ICache的实现
type ProvCache struct{}

//KeyName KeyName
func (m *ProvCache) KeyName() string {
    return "prov"
}

//PrimaryKey PrimaryKey
func (m *ProvCache) PrimaryKey(obj interface{}) string {
    return fmt.Sprintf("%v", obj.(*Prov).ID)
}

//GetAllData
func (m *ProvCache) GetAllData() (data interface{}, err error) {
    data, err = new(Prov).GetAllDataFromDB()
    return
}

//===================使用者===============

//所有prov数据
func TestCacheUtil_GetAll(t *testing.T) {
    nc := NewMemCache()
    data, err := nc.GetAll(new(ProvCache))
    if err != nil {
        t.Fatal(err)
    }

    fmt.Println("===test all===")

    //返回值断言
    value, ok := data.([]*Prov)
    if ok {
        fmt.Printf("value: %#v \n", value)
        for _, item := range value {
            fmt.Println(item)
        }
    }

}

//单个prov数据
func TestCacheUtil_GetItem(t *testing.T) {
    nc := NewMemCache()
    data, err := nc.GetItem(new(ProvCache), 51)
    if err != nil {
        t.Fatal(err)
    }

    fmt.Println("===test item===")
    value, ok := data.(*Prov)
    if ok {
        fmt.Println(value)
    }

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ICache

type ICache interface {
	//KeyName key
	KeyName() string

	//PrimaryKey 主键名称,
	PrimaryKey(model interface{}) string

	//GetAllData 获取所有数据
	GetAllData() (data interface{}, err error)
}

ICache 缓存接口

type MemCache

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

MemCache MemCache

func NewMemCache

func NewMemCache() *MemCache

NewMemCache NewMemCache

func (*MemCache) GetAll

func (m *MemCache) GetAll(obj ICache) (data interface{}, err error)

GetAll 获取obj的所有缓存数据

func (*MemCache) GetItem

func (m *MemCache) GetItem(obj ICache, id int64) (item interface{}, err error)

GetItem 取[]data中的一项数据

func (*MemCache) Remove

func (m *MemCache) Remove(obj ICache)

Remove 移出key对应的缓存

Jump to

Keyboard shortcuts

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