cachedb

package
v0.0.0-...-1ec2636 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2019 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

带redis缓存的数据库对象操作模块 使用者不需要对缓存进行管理,只需要实现从mysql读写数据的接口即可

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheDB

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

func New

func New(mysqldb *mysql.MysqlDB, cache *redis.RedisPool, rdbid int) *CacheDB

新建CacheDB对象

func (*CacheDB) ClearCache

func (db *CacheDB) ClearCache(obj DBObject) (e error)

清除缓存

func (*CacheDB) Get

func (db *CacheDB) Get(id interface{}, data DBObject) (e error)

读取数据,redis里有则从redis读取,否则从mysql读取,同时放入redis缓存

func (*CacheDB) GetMap

func (db *CacheDB) GetMap(data map[interface{}]DBObject, newDBObject func(id interface{}) DBObject) (e error)

批量读取数据,策略与Get一样 newDBObject函数用来生成用户自定义的DBObject

func (*CacheDB) Save

func (db *CacheDB) Save(data DBObject) (id interface{}, e error)

新增或更新数据,如果redis里有缓存,也一并更新

type DBObject

type DBObject interface {
	//返回对象的ID,如果没有设置ID,则第二个返回值为false
	ID() (interface{}, bool)
	//缓存超时时间(秒),-1表示不超时,0表示默认值10分钟
	Expire() int

	//新增或更新数据
	Save(mysqldb *mysql.MysqlDB) (id interface{}, e error)
	//获取数据内容
	Get(id interface{}, mysqldb *mysql.MysqlDB) (e error)
	//批量从数据库取数据
	GetMap(ids []interface{}, mysqldb *mysql.MysqlDB) (objs map[interface{}]DBObject, e error)
}

Jump to

Keyboard shortcuts

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