mirror of
https://github.com/go-micro/go-micro.git
synced 2025-02-04 18:21:53 +02:00
Add cache example (#2232)
This commit is contained in:
parent
05a299b76c
commit
ed9053ed94
@ -5,6 +5,7 @@ This is a repository for go-micro examples. Feel free to contribute.
|
||||
## Contents
|
||||
|
||||
- [broker](broker) - A example of using Broker for Publish and Subscribing.
|
||||
- [cache](cache) - Usage of the Cache package to create a cache service.
|
||||
- [client](client) - Usage of the Client package to call a service.
|
||||
- [config](config) - Using Go Config for dynamic config
|
||||
- [event](event) - Using the API Gateway event handler
|
||||
|
47
examples/cache/README.md
vendored
Normal file
47
examples/cache/README.md
vendored
Normal file
@ -0,0 +1,47 @@
|
||||
# Cache
|
||||
|
||||
This is an example of a cache service using the [asim/go-micro/cache][1] package.
|
||||
|
||||
## Contents
|
||||
|
||||
This project was generated using [Gomu][2].
|
||||
|
||||
* handler - contains the service handler
|
||||
* proto - contains the protocol buffer and generated code
|
||||
|
||||
## Usage
|
||||
|
||||
Run the `go.micro.srv.cache` service using [Gomu][2].
|
||||
|
||||
```bash
|
||||
gomu run
|
||||
```
|
||||
|
||||
You can also run it using plain Go.
|
||||
|
||||
```bash
|
||||
go run main.go
|
||||
```
|
||||
|
||||
We'll be using [Gomu][2] to call the service. You can store a new key-value
|
||||
pair in the cache.
|
||||
|
||||
```bash
|
||||
gomu call go.micro.srv.cache Cache.Put '{"key":"test","value":"hello go-micro","duration":"12h"}'
|
||||
```
|
||||
|
||||
You can get values from the cache.
|
||||
|
||||
```bash
|
||||
$ gomu call go.micro.srv.cache Cache.Get '{"key":"test"}'
|
||||
{"expiration":"2021-09-01 22:42:24.2370591 +0200 CEST","value":"hello go-micro"}
|
||||
```
|
||||
|
||||
Finally you can delete keys from the cache.
|
||||
|
||||
```bash
|
||||
gomu call go.micro.srv.cache Cache.Delete '{"key":"test"}'
|
||||
```
|
||||
|
||||
[1]: https://github.com/asim/go-micro/tree/master/cache
|
||||
[2]: https://github.com/asim/go-micro/tree/master/cmd/gomu
|
60
examples/cache/handler/cache.go
vendored
Normal file
60
examples/cache/handler/cache.go
vendored
Normal file
@ -0,0 +1,60 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/asim/go-micro/v3/cache"
|
||||
log "github.com/asim/go-micro/v3/logger"
|
||||
|
||||
pb "github.com/asim/go-micro/examples/v3/cache/proto"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewCache(opts ...cache.Option) *Cache {
|
||||
c := cache.NewCache(opts...)
|
||||
return &Cache{c}
|
||||
}
|
||||
|
||||
func (c *Cache) Get(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse) error {
|
||||
log.Infof("Received Cache.Get request: %v", req)
|
||||
|
||||
v, e, err := c.cache.Context(ctx).Get(req.Key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rsp.Value = fmt.Sprintf("%v", v)
|
||||
rsp.Expiration = e.String()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) Put(ctx context.Context, req *pb.PutRequest, rsp *pb.PutResponse) error {
|
||||
log.Infof("Received Cache.Put request: %v", req)
|
||||
|
||||
d, err := time.ParseDuration(req.Duration)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.cache.Context(ctx).Put(req.Key, req.Value, d); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cache) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||
log.Infof("Received Cache.Delete request: %v", req)
|
||||
|
||||
if err := c.cache.Context(ctx).Delete(req.Key); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
31
examples/cache/main.go
vendored
Normal file
31
examples/cache/main.go
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/asim/go-micro/examples/v3/cache/handler"
|
||||
pb "github.com/asim/go-micro/examples/v3/cache/proto"
|
||||
|
||||
"github.com/asim/go-micro/v3"
|
||||
log "github.com/asim/go-micro/v3/logger"
|
||||
)
|
||||
|
||||
var (
|
||||
service = "go.micro.srv.cache"
|
||||
version = "latest"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create service
|
||||
srv := micro.NewService(
|
||||
micro.Name(service),
|
||||
micro.Version(version),
|
||||
)
|
||||
srv.Init()
|
||||
|
||||
// Register handler
|
||||
pb.RegisterCacheHandler(srv.Server(), handler.NewCache())
|
||||
|
||||
// Run service
|
||||
if err := srv.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
486
examples/cache/proto/cache.pb.go
vendored
Normal file
486
examples/cache/proto/cache.pb.go
vendored
Normal file
@ -0,0 +1,486 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.26.0
|
||||
// protoc v3.6.1
|
||||
// source: proto/cache.proto
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type GetRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// The key to fetch from the cache.
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetRequest) Reset() {
|
||||
*x = GetRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetRequest) ProtoMessage() {}
|
||||
|
||||
func (x *GetRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetRequest.ProtoReflect.Descriptor instead.
|
||||
func (*GetRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
func (x *GetRequest) GetKey() string {
|
||||
if x != nil {
|
||||
return x.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type GetResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// The value retrieved from the cache.
|
||||
Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"`
|
||||
// The value's expiration datetime.
|
||||
Expiration string `protobuf:"bytes,2,opt,name=expiration,proto3" json:"expiration,omitempty"`
|
||||
}
|
||||
|
||||
func (x *GetResponse) Reset() {
|
||||
*x = GetResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *GetResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*GetResponse) ProtoMessage() {}
|
||||
|
||||
func (x *GetResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use GetResponse.ProtoReflect.Descriptor instead.
|
||||
func (*GetResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *GetResponse) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *GetResponse) GetExpiration() string {
|
||||
if x != nil {
|
||||
return x.Expiration
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type PutRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// The key to store in the cache.
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
// The value to store in the cache.
|
||||
Value string `protobuf:"bytes,2,opt,name=value,proto3" json:"value,omitempty"`
|
||||
// The value's time to live. Parsed using time.ParseDuration. 0 means
|
||||
// it doesn't expire.
|
||||
Duration string `protobuf:"bytes,3,opt,name=duration,proto3" json:"duration,omitempty"`
|
||||
}
|
||||
|
||||
func (x *PutRequest) Reset() {
|
||||
*x = PutRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PutRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PutRequest) ProtoMessage() {}
|
||||
|
||||
func (x *PutRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PutRequest.ProtoReflect.Descriptor instead.
|
||||
func (*PutRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *PutRequest) GetKey() string {
|
||||
if x != nil {
|
||||
return x.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PutRequest) GetValue() string {
|
||||
if x != nil {
|
||||
return x.Value
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *PutRequest) GetDuration() string {
|
||||
if x != nil {
|
||||
return x.Duration
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type PutResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *PutResponse) Reset() {
|
||||
*x = PutResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *PutResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*PutResponse) ProtoMessage() {}
|
||||
|
||||
func (x *PutResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use PutResponse.ProtoReflect.Descriptor instead.
|
||||
func (*PutResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
type DeleteRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// The key to remove from the cache.
|
||||
Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"`
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) Reset() {
|
||||
*x = DeleteRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*DeleteRequest) ProtoMessage() {}
|
||||
|
||||
func (x *DeleteRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[4]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use DeleteRequest.ProtoReflect.Descriptor instead.
|
||||
func (*DeleteRequest) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *DeleteRequest) GetKey() string {
|
||||
if x != nil {
|
||||
return x.Key
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type DeleteResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *DeleteResponse) Reset() {
|
||||
*x = DeleteResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_proto_cache_proto_msgTypes[5]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *DeleteResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*DeleteResponse) ProtoMessage() {}
|
||||
|
||||
func (x *DeleteResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_proto_cache_proto_msgTypes[5]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use DeleteResponse.ProtoReflect.Descriptor instead.
|
||||
func (*DeleteResponse) Descriptor() ([]byte, []int) {
|
||||
return file_proto_cache_proto_rawDescGZIP(), []int{5}
|
||||
}
|
||||
|
||||
var File_proto_cache_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_proto_cache_proto_rawDesc = []byte{
|
||||
0x0a, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x12, 0x05, 0x63, 0x61, 0x63, 0x68, 0x65, 0x22, 0x1e, 0x0a, 0x0a, 0x47, 0x65,
|
||||
0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18,
|
||||
0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0x43, 0x0a, 0x0b, 0x47, 0x65,
|
||||
0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c,
|
||||
0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12,
|
||||
0x1e, 0x0a, 0x0a, 0x65, 0x78, 0x70, 0x69, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x0a, 0x65, 0x78, 0x70, 0x69, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22,
|
||||
0x50, 0x0a, 0x0a, 0x50, 0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x10, 0x0a,
|
||||
0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x12,
|
||||
0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05,
|
||||
0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f,
|
||||
0x6e, 0x22, 0x0d, 0x0a, 0x0b, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
|
||||
0x22, 0x21, 0x0a, 0x0d, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
|
||||
0x74, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03,
|
||||
0x6b, 0x65, 0x79, 0x22, 0x10, 0x0a, 0x0e, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x73,
|
||||
0x70, 0x6f, 0x6e, 0x73, 0x65, 0x32, 0xa0, 0x01, 0x0a, 0x05, 0x43, 0x61, 0x63, 0x68, 0x65, 0x12,
|
||||
0x2e, 0x0a, 0x03, 0x47, 0x65, 0x74, 0x12, 0x11, 0x2e, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x47,
|
||||
0x65, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x63, 0x61, 0x63, 0x68,
|
||||
0x65, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12,
|
||||
0x2e, 0x0a, 0x03, 0x50, 0x75, 0x74, 0x12, 0x11, 0x2e, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x50,
|
||||
0x75, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x63, 0x61, 0x63, 0x68,
|
||||
0x65, 0x2e, 0x50, 0x75, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12,
|
||||
0x37, 0x0a, 0x06, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x63, 0x61, 0x63, 0x68,
|
||||
0x65, 0x2e, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a,
|
||||
0x15, 0x2e, 0x63, 0x61, 0x63, 0x68, 0x65, 0x2e, 0x44, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x52, 0x65,
|
||||
0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x2f, 0x70, 0x72,
|
||||
0x6f, 0x74, 0x6f, 0x3b, 0x63, 0x61, 0x63, 0x68, 0x65, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_proto_cache_proto_rawDescOnce sync.Once
|
||||
file_proto_cache_proto_rawDescData = file_proto_cache_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_proto_cache_proto_rawDescGZIP() []byte {
|
||||
file_proto_cache_proto_rawDescOnce.Do(func() {
|
||||
file_proto_cache_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_cache_proto_rawDescData)
|
||||
})
|
||||
return file_proto_cache_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_proto_cache_proto_msgTypes = make([]protoimpl.MessageInfo, 6)
|
||||
var file_proto_cache_proto_goTypes = []interface{}{
|
||||
(*GetRequest)(nil), // 0: cache.GetRequest
|
||||
(*GetResponse)(nil), // 1: cache.GetResponse
|
||||
(*PutRequest)(nil), // 2: cache.PutRequest
|
||||
(*PutResponse)(nil), // 3: cache.PutResponse
|
||||
(*DeleteRequest)(nil), // 4: cache.DeleteRequest
|
||||
(*DeleteResponse)(nil), // 5: cache.DeleteResponse
|
||||
}
|
||||
var file_proto_cache_proto_depIdxs = []int32{
|
||||
0, // 0: cache.Cache.Get:input_type -> cache.GetRequest
|
||||
2, // 1: cache.Cache.Put:input_type -> cache.PutRequest
|
||||
4, // 2: cache.Cache.Delete:input_type -> cache.DeleteRequest
|
||||
1, // 3: cache.Cache.Get:output_type -> cache.GetResponse
|
||||
3, // 4: cache.Cache.Put:output_type -> cache.PutResponse
|
||||
5, // 5: cache.Cache.Delete:output_type -> cache.DeleteResponse
|
||||
3, // [3:6] is the sub-list for method output_type
|
||||
0, // [0:3] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_proto_cache_proto_init() }
|
||||
func file_proto_cache_proto_init() {
|
||||
if File_proto_cache_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_proto_cache_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_cache_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*GetResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_cache_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PutRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_cache_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*PutResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_cache_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DeleteRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_proto_cache_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*DeleteResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_proto_cache_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 6,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_proto_cache_proto_goTypes,
|
||||
DependencyIndexes: file_proto_cache_proto_depIdxs,
|
||||
MessageInfos: file_proto_cache_proto_msgTypes,
|
||||
}.Build()
|
||||
File_proto_cache_proto = out.File
|
||||
file_proto_cache_proto_rawDesc = nil
|
||||
file_proto_cache_proto_goTypes = nil
|
||||
file_proto_cache_proto_depIdxs = nil
|
||||
}
|
127
examples/cache/proto/cache.pb.micro.go
vendored
Normal file
127
examples/cache/proto/cache.pb.micro.go
vendored
Normal file
@ -0,0 +1,127 @@
|
||||
// Code generated by protoc-gen-micro. DO NOT EDIT.
|
||||
// source: proto/cache.proto
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
fmt "fmt"
|
||||
proto "google.golang.org/protobuf/proto"
|
||||
math "math"
|
||||
)
|
||||
|
||||
import (
|
||||
context "context"
|
||||
api "github.com/asim/go-micro/v3/api"
|
||||
client "github.com/asim/go-micro/v3/client"
|
||||
server "github.com/asim/go-micro/v3/server"
|
||||
)
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ = proto.Marshal
|
||||
var _ = fmt.Errorf
|
||||
var _ = math.Inf
|
||||
|
||||
// Reference imports to suppress errors if they are not otherwise used.
|
||||
var _ api.Endpoint
|
||||
var _ context.Context
|
||||
var _ client.Option
|
||||
var _ server.Option
|
||||
|
||||
// Api Endpoints for Cache service
|
||||
|
||||
func NewCacheEndpoints() []*api.Endpoint {
|
||||
return []*api.Endpoint{}
|
||||
}
|
||||
|
||||
// Client API for Cache service
|
||||
|
||||
type CacheService interface {
|
||||
// Get receives a value and its time to live by key from the cache.
|
||||
Get(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error)
|
||||
// Put stores a value and its time to live in the cache.
|
||||
Put(ctx context.Context, in *PutRequest, opts ...client.CallOption) (*PutResponse, error)
|
||||
// Delete removes a key from the cache.
|
||||
Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error)
|
||||
}
|
||||
|
||||
type cacheService struct {
|
||||
c client.Client
|
||||
name string
|
||||
}
|
||||
|
||||
func NewCacheService(name string, c client.Client) CacheService {
|
||||
return &cacheService{
|
||||
c: c,
|
||||
name: name,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cacheService) Get(ctx context.Context, in *GetRequest, opts ...client.CallOption) (*GetResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Cache.Get", in)
|
||||
out := new(GetResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *cacheService) Put(ctx context.Context, in *PutRequest, opts ...client.CallOption) (*PutResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Cache.Put", in)
|
||||
out := new(PutResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *cacheService) Delete(ctx context.Context, in *DeleteRequest, opts ...client.CallOption) (*DeleteResponse, error) {
|
||||
req := c.c.NewRequest(c.name, "Cache.Delete", in)
|
||||
out := new(DeleteResponse)
|
||||
err := c.c.Call(ctx, req, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Server API for Cache service
|
||||
|
||||
type CacheHandler interface {
|
||||
// Get receives a value and its time to live by key from the cache.
|
||||
Get(context.Context, *GetRequest, *GetResponse) error
|
||||
// Put stores a value and its time to live in the cache.
|
||||
Put(context.Context, *PutRequest, *PutResponse) error
|
||||
// Delete removes a key from the cache.
|
||||
Delete(context.Context, *DeleteRequest, *DeleteResponse) error
|
||||
}
|
||||
|
||||
func RegisterCacheHandler(s server.Server, hdlr CacheHandler, opts ...server.HandlerOption) error {
|
||||
type cache interface {
|
||||
Get(ctx context.Context, in *GetRequest, out *GetResponse) error
|
||||
Put(ctx context.Context, in *PutRequest, out *PutResponse) error
|
||||
Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error
|
||||
}
|
||||
type Cache struct {
|
||||
cache
|
||||
}
|
||||
h := &cacheHandler{hdlr}
|
||||
return s.Handle(s.NewHandler(&Cache{h}, opts...))
|
||||
}
|
||||
|
||||
type cacheHandler struct {
|
||||
CacheHandler
|
||||
}
|
||||
|
||||
func (h *cacheHandler) Get(ctx context.Context, in *GetRequest, out *GetResponse) error {
|
||||
return h.CacheHandler.Get(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *cacheHandler) Put(ctx context.Context, in *PutRequest, out *PutResponse) error {
|
||||
return h.CacheHandler.Put(ctx, in, out)
|
||||
}
|
||||
|
||||
func (h *cacheHandler) Delete(ctx context.Context, in *DeleteRequest, out *DeleteResponse) error {
|
||||
return h.CacheHandler.Delete(ctx, in, out)
|
||||
}
|
46
examples/cache/proto/cache.proto
vendored
Normal file
46
examples/cache/proto/cache.proto
vendored
Normal file
@ -0,0 +1,46 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package cache;
|
||||
|
||||
option go_package = "./proto;cache";
|
||||
|
||||
// Cache offers a cache as a service functionality.
|
||||
service Cache {
|
||||
// Get receives a value and its time to live by key from the cache.
|
||||
rpc Get(GetRequest) returns (GetResponse) {}
|
||||
// Put stores a value and its time to live in the cache.
|
||||
rpc Put(PutRequest) returns (PutResponse) {}
|
||||
// Delete removes a key from the cache.
|
||||
rpc Delete(DeleteRequest) returns (DeleteResponse) {}
|
||||
}
|
||||
|
||||
message GetRequest {
|
||||
// The key to fetch from the cache.
|
||||
string key = 1;
|
||||
}
|
||||
|
||||
message GetResponse {
|
||||
// The value retrieved from the cache.
|
||||
string value = 1;
|
||||
// The value's expiration datetime.
|
||||
string expiration = 2;
|
||||
}
|
||||
|
||||
message PutRequest {
|
||||
// The key to store in the cache.
|
||||
string key = 1;
|
||||
// The value to store in the cache.
|
||||
string value = 2;
|
||||
// The value's time to live. Parsed using time.ParseDuration. 0 means
|
||||
// it doesn't expire.
|
||||
string duration = 3;
|
||||
}
|
||||
|
||||
message PutResponse {}
|
||||
|
||||
message DeleteRequest {
|
||||
// The key to remove from the cache.
|
||||
string key = 1;
|
||||
}
|
||||
|
||||
message DeleteResponse {}
|
@ -10,14 +10,15 @@ require (
|
||||
github.com/asim/go-micro/plugins/server/http/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/plugins/wrapper/select/shard/v3 v3.0.0-20210403073940-e7a7e3a05092
|
||||
github.com/asim/go-micro/v3 v3.5.2-0.20210630062103-c13bb07171bc
|
||||
github.com/asim/go-micro/v3 v3.6.1-0.20210831143116-05a299b76c7c
|
||||
github.com/gin-gonic/gin v1.7.0
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
|
||||
github.com/golang/protobuf v1.5.2
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0
|
||||
github.com/micro/cli/v2 v2.1.2
|
||||
github.com/pborman/uuid v1.2.1
|
||||
github.com/urfave/cli/v2 v2.3.0
|
||||
golang.org/x/net v0.0.0-20210510120150-4163338589ed
|
||||
google.golang.org/genproto v0.0.0-20210624195500-8bfb893ecb84
|
||||
google.golang.org/grpc v1.38.0
|
||||
google.golang.org/protobuf v1.26.0
|
||||
)
|
||||
|
@ -102,8 +102,9 @@ github.com/asim/go-micro/plugins/wrapper/select/roundrobin/v3 v3.0.0-20210403073
|
||||
github.com/asim/go-micro/plugins/wrapper/select/shard/v3 v3.0.0-20210403073940-e7a7e3a05092 h1:YU1Qks1LnRQU/b9UbHz0V1ZMjlWcuQmPf95g403VRiY=
|
||||
github.com/asim/go-micro/plugins/wrapper/select/shard/v3 v3.0.0-20210403073940-e7a7e3a05092/go.mod h1:W82nHY+YFENWaye4jazv6nX+OYru9T7It+wUqJA9Vc0=
|
||||
github.com/asim/go-micro/v3 v3.0.0-20210120135431-d94936f6c97c/go.mod h1:fAeb2KUnD3z4XwtQ91XFxw5zgGKhoTsLc6Ie81QdER4=
|
||||
github.com/asim/go-micro/v3 v3.5.2-0.20210630062103-c13bb07171bc h1:nbM89BiCBeWjJESWmf8BQj8ObK6HEQzc/Dj7uITXR/8=
|
||||
github.com/asim/go-micro/v3 v3.5.2-0.20210630062103-c13bb07171bc/go.mod h1:cNGIIYQcp0qy+taNYmrBdaIHeqMWHV5ZH/FfQzfOyE8=
|
||||
github.com/asim/go-micro/v3 v3.6.1-0.20210831143116-05a299b76c7c h1:CaJvJPUEbd5aBmCBhOHmWDHCQr2HCxyCkbBxHW7w/kY=
|
||||
github.com/asim/go-micro/v3 v3.6.1-0.20210831143116-05a299b76c7c/go.mod h1:UQd2JfP/+4goQxddksr2r6ZSaUXNIlVE8hlepbAx/DI=
|
||||
github.com/aws/aws-sdk-go v1.23.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
|
||||
github.com/aws/aws-sdk-go v1.37.27/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
|
||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f/go.mod h1:AuiFmCCPBSrqvVMvuqFuk0qogytodnVFVSN5CeJB8Gc=
|
||||
@ -466,7 +467,6 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
|
||||
github.com/mattn/go-tty v0.0.0-20180219170247-931426f7535a/go.mod h1:XPvLUNfbS4fJH25nqRHfWLMa1ONC8Amw+mIA639KxkE=
|
||||
github.com/mattn/go-tty v0.0.3/go.mod h1:ihxohKRERHTVzN+aSVRwACLCeqIoZAWpoICkkvrWyR0=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/micro/cli/v2 v2.1.2 h1:43J1lChg/rZCC1rvdqZNFSQDrGT7qfMrtp6/ztpIkEM=
|
||||
github.com/micro/cli/v2 v2.1.2/go.mod h1:EguNh6DAoWKm9nmk+k/Rg0H3lQnDxqzu5x5srOtGtYg=
|
||||
github.com/microcosm-cc/bluemonday v1.0.1/go.mod h1:hsXNsILzKxV+sX77C5b8FSuKF00vh2OMYv+xgHpAMF4=
|
||||
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
@ -679,7 +679,9 @@ github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLY
|
||||
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
|
||||
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
github.com/urfave/cli v1.22.4 h1:u7tSpNPPswAFymm8IehJhy4uJMlUuU/GmqSkvJ1InXA=
|
||||
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
|
||||
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
|
||||
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
|
||||
|
Loading…
x
Reference in New Issue
Block a user