Skip to content
/ cache Public
forked from seaguest/cache

A distributed two-level cache (memory + redis) with loader function library for Go.

License

Notifications You must be signed in to change notification settings

codecopy/cache

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cache

A distributed two-level cache (memory + redis) with loader function library for Go.

Mem cache is built on top of sync.Map.

When cache.Delete(key) is called, redis will publish to all cache nodes, then an delete action (mem+redis) is performed by each cache node.

Usage:

package main

import (
	"github.com/jinzhu/gorm"
	"github.com/seaguest/cache"
	"github.com/seaguest/common/logger"
)

func getVal(id uint32, db *gorm.DB) (uint32, error) {
	key := cache.GetCacheKey("val", id)
	var v uint32
	err := cache.GetCacheObject(key, &v, 60, func() (interface{}, error) {
		// DB query
		var res uint32 = 100
		return res, nil
	})
	if err != nil {
		logger.Error(err)
		return 0, err
	}
	return v, nil
}

func main() {
	cache.Init("127.0.0.1:6379", "", true, 200)

	v, e := getVal(100, nil)
	logger.Error(v, e)
}

About

A distributed two-level cache (memory + redis) with loader function library for Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%