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 ¶
- type Cache
- type Control
- type Degrader
- type Filter
- type HeaderValue
- type Page
- type Policy
- type ResponseCache
- func (*ResponseCache) Descriptor() ([]byte, []int)
- func (m *ResponseCache) GetData() []byte
- func (m *ResponseCache) GetHeader() map[string]*HeaderValue
- func (m *ResponseCache) GetStatus() int32
- func (*ResponseCache) ProtoMessage()
- func (m *ResponseCache) Reset()
- func (m *ResponseCache) String() string
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
type Control ¶
type Control struct { MaxAge int32 // contains filtered or unexported fields }
Control is used to work as client side cache orchestrator
func NewControl ¶
NewControl will create a new control cache struct
type Degrader ¶
type Degrader struct {
// contains filtered or unexported fields
}
Degrader is the common degrader instance.
func NewDegrader ¶
NewDegrader will create a new degrade struct
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
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