enablecache

command module
v0.0.0-...-107dafa Latest Latest
Warning

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

Go to latest
Published: May 23, 2016 License: Apache-2.0 Imports: 0 Imported by: 0

README

#EnableCache lib. Wiki GoDoc Build Status

Allow to enable cache in almost any golang function easily.

Minimum example

package main

import (
	"fmt"
	"strconv"
	
	"github.com/darciopacifico/enablecache/aop"
	"github.com/darciopacifico/enablecache/cache"
)

//concrete no cached function
func FindProduct(id int) string {
	fmt.Println("calling a very expensive function...")
	return "product:" + strconv.Itoa(id)
}

//empty function, currently pointing to nil, that will receive cache spot, with same signature of FindProduct
var CachedFindProduct func(id int) string

//cache spot configuration
var cacheSpot aop.CacheSpot

//initialize cache
func init(){
	//cache manager that will intermediate all operations for cache store/read.
	cacheManager := cache.SimpleCacheManager{
		CacheStorage: cache.NewRedisCacheStorage("localhost:6379", "", 8, "lab"),
	}

	//start cache spot reference.
	cacheSpot = aop.CacheSpot{
		HotFunc: FindProduct,		// concrete FindProduct function
		CachedFunc: &CachedFindProduct, // Empty cached function as ref. Will receive a swap function
		CacheManager: cacheManager,	// Cache Manager implementation
	}.MustStartCache()			// Validate function signatures, assoaciate swap to CachedFunc
}

func main() {
	//call new cached find product as usually call original FindProduct
	fmt.Println(CachedFindProduct(9))

	//Cache storage operations is started in separateds go routines.
	//A waiting group ensure for all operations to finish.
	cacheSpot.WaitAllParallelOps()
}
  • It's important to call cacheSpot.MustStartCache() at an func init(){...}. It's need to fail at startup if some cache config goes wrong!

  • Check your Redis registries after. Some new keys was stored.

  • Call CachedFindProduct many times and note that the fake "expensive operation" will not be called anymore, until cache expires.

  • Allways call cacheSpot.WaitAllParallelOps() at the end of yor program, or when need to sincronize pending store operations.

Used in Production

Currently in production in some big retailer e-commerce environment that i have worked to ;-)

Performance

  • Proven performance for at least 300 simultaneous requests per second on 1Gb RAM and 1 CPU Core, with no leaks and minimum CPU overhead.

Detailed function

  • Independent and cohesive layers, with well defined interfaces.
    • Cache Spot: AOP like instrumentation, allowing almost any golang function to be transparently cached.
    • Cache Manager: implements cache split algorithm.
    • Cache Storage: interact with an external big memory layer (Redis).

License

  • Enablecache is free software, licensed under the Apache License, Version 2.0 (the "License"). Commercial and non-commercial use are permitted in compliance with the License.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package AOP contains the mechanism to bind a Cache Spot to a Go Function, starting from aop.CacheSpot struct.
Package AOP contains the mechanism to bind a Cache Spot to a Go Function, starting from aop.CacheSpot struct.
Package Cache contains the CacheManager and CacheStorage formal interface definitions, a SimpleCacheManager, AutoCacheManager and RedisCacheStorage implementations.
Package Cache contains the CacheManager and CacheStorage formal interface definitions, a SimpleCacheManager, AutoCacheManager and RedisCacheStorage implementations.
Package the executable example using redis and simple cache manager.
Package the executable example using redis and simple cache manager.

Jump to

Keyboard shortcuts

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