cache

package
v0.0.0-...-83adff0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2020 License: GPL-3.0 Imports: 19 Imported by: 0

README

business/blademaster/cache

项目简介

blademaster 的通用 cache 模块,一般直接用于缓存返回的 response

编译环境

  • 请只用 Golang v1.8.x 以上版本编译执行

依赖包

  • No other dependency

Documentation

Overview

Package cache is a generated protocol buffer package.

It is generated from these files:

page.proto

It has these top-level messages:

ResponseCache
HeaderValue
Example

This example create a cache middleware instance and two cache policy, then attach them to the specified path.

The `PageCache` policy will attempt to cache the whole response by URI. It usually used to cache the common response.

The `Degrader` policy usually used to prevent the API totaly unavailable if any disaster is happen. A succeeded response will be cached per 600s. The cache key is generated by specified args and its values. You can using file or memcache as cache backend for degradation currently.

The `Cache` policy is used to work with multilevel HTTP caching architecture. This will cause client side response caching. We only support weak validator with `ETag` header currently.

package main

import (
	"time"

	"go-common/library/cache/memcache"
	"go-common/library/container/pool"
	"go-common/library/ecode"
	"go-common/library/net/http/blademaster"
	"go-common/library/net/http/blademaster/middleware/cache"
	"go-common/library/net/http/blademaster/middleware/cache/store"

	xtime "go-common/library/time"

	"github.com/pkg/errors"
)

func main() {
	mc := store.NewMemcache(&memcache.Config{
		Config: &pool.Config{
			Active:      10,
			Idle:        2,
			IdleTimeout: xtime.Duration(time.Second),
		},
		Name:         "test",
		Proto:        "tcp",
		Addr:         "172.16.33.54:11211",
		DialTimeout:  xtime.Duration(time.Second),
		ReadTimeout:  xtime.Duration(time.Second),
		WriteTimeout: xtime.Duration(time.Second),
	})
	ca := cache.New(mc)
	deg := cache.NewDegrader(10)
	pc := cache.NewPage(10)
	ctl := cache.NewControl(10)
	filter := func(ctx *blademaster.Context) bool {
		if ctx.Request.Form.Get("cache") == "false" {
			return false
		}
		return true
	}

	engine := blademaster.Default()
	engine.GET("/users/profile", ca.Cache(deg.Args("name", "age"), nil), func(c *blademaster.Context) {
		values := c.Request.URL.Query()
		name := values.Get("name")
		age := values.Get("age")

		err := errors.New("error from others") // error from other call
		if err != nil {
			// mark this response should be degraded
			c.JSON(nil, ecode.Degrade)
			return
		}
		c.JSON(map[string]string{"name": name, "age": age}, nil)
	})
	engine.GET("/users/index", ca.Cache(pc, nil), func(c *blademaster.Context) {
		c.String(200, "%s", "Title: User")
	})
	engine.GET("/users/list", ca.Cache(ctl, filter), func(c *blademaster.Context) {
		c.JSON([]string{"user1", "user2", "user3"}, nil)
	})
	engine.Run(":18080")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is the abstract struct for any cache impl

func New

func New(store store.Store) *Cache

New will create a new Cache struct

func (*Cache) Cache

func (c *Cache) Cache(policy Policy, filter Filter) bm.HandlerFunc

Cache is used to mark path as customized cache policy

type Control

type Control struct {
	MaxAge int32
	// contains filtered or unexported fields
}

Control is used to work as client side cache orchestrator

func NewControl

func NewControl(maxAge int32) *Control

NewControl will create a new control cache struct

func (*Control) Handler

func (ctl *Control) Handler(_ store.Store) bm.HandlerFunc

Handler is used to execute cache service

func (*Control) Key

func (ctl *Control) Key(ctx *bm.Context) string

Key method is not needed in this situation

type Degrader

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

Degrader is the common degrader instance.

func NewDegrader

func NewDegrader(expire int32) (d *Degrader)

NewDegrader will create a new degrade struct

func (*Degrader) Args

func (d *Degrader) Args(args ...string) Policy

Args means this path will be degrade by specified args

type Filter

type Filter func(*bm.Context) bool

Filter is used to check is cache required for every request

type HeaderValue

type HeaderValue struct {
	Value []string `protobuf:"bytes,1,rep,name=Value" json:"Value,omitempty"`
}

func (*HeaderValue) Descriptor

func (*HeaderValue) Descriptor() ([]byte, []int)

func (*HeaderValue) GetValue

func (m *HeaderValue) GetValue() []string

func (*HeaderValue) ProtoMessage

func (*HeaderValue) ProtoMessage()

func (*HeaderValue) Reset

func (m *HeaderValue) Reset()

func (*HeaderValue) String

func (m *HeaderValue) String() string

type Page

type Page struct {
	Expire int32
	// contains filtered or unexported fields
}

Page is used to cache common response

func NewPage

func NewPage(expire int32) *Page

NewPage will create a new page cache struct

func (*Page) Handler

func (p *Page) Handler(store store.Store) bm.HandlerFunc

Handler is used to execute cache service

func (*Page) Key

func (p *Page) Key(ctx *bm.Context) string

Key is used to identify response cache key in most key-value store

type Policy

type Policy interface {
	Key(*bm.Context) string
	Handler(store.Store) bm.HandlerFunc
}

Policy is used to abstract different cache policy

type ResponseCache

type ResponseCache struct {
	Status int32                   `protobuf:"varint,1,opt,name=Status,proto3" json:"Status,omitempty"`
	Header map[string]*HeaderValue `` /* 139-byte string literal not displayed */
	Data   []byte                  `protobuf:"bytes,3,opt,name=Data,proto3" json:"Data,omitempty"`
}

func (*ResponseCache) Descriptor

func (*ResponseCache) Descriptor() ([]byte, []int)

func (*ResponseCache) GetData

func (m *ResponseCache) GetData() []byte

func (*ResponseCache) GetHeader

func (m *ResponseCache) GetHeader() map[string]*HeaderValue

func (*ResponseCache) GetStatus

func (m *ResponseCache) GetStatus() int32

func (*ResponseCache) ProtoMessage

func (*ResponseCache) ProtoMessage()

func (*ResponseCache) Reset

func (m *ResponseCache) Reset()

func (*ResponseCache) String

func (m *ResponseCache) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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