mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-23 15:51:24 +00:00
Fix cache get for key not found (#254)
This commit is contained in:
20
cache/handler/cache.go
vendored
20
cache/handler/cache.go
vendored
@@ -3,9 +3,11 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/micro/micro/v3/service/errors"
|
"github.com/micro/micro/v3/service/errors"
|
||||||
|
log "github.com/micro/micro/v3/service/logger"
|
||||||
pb "github.com/micro/services/cache/proto"
|
pb "github.com/micro/services/cache/proto"
|
||||||
"github.com/micro/services/pkg/cache"
|
"github.com/micro/services/pkg/cache"
|
||||||
)
|
)
|
||||||
@@ -20,7 +22,11 @@ func (c *Cache) Get(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse
|
|||||||
var value interface{}
|
var value interface{}
|
||||||
|
|
||||||
if err := cache.Context(ctx).Get(req.Key, &value); err != nil {
|
if err := cache.Context(ctx).Get(req.Key, &value); err != nil {
|
||||||
return errors.InternalServerError("cache.get", err.Error())
|
if !strings.Contains(err.Error(), "not found") {
|
||||||
|
log.Errorf("Error querying cache %s", err)
|
||||||
|
return errors.InternalServerError("cache.get", "Error querying cache")
|
||||||
|
}
|
||||||
|
value = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp.Key = req.Key
|
rsp.Key = req.Key
|
||||||
@@ -47,7 +53,8 @@ func (c *Cache) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := cache.Context(ctx).Set(req.Key, req.Value, ttl); err != nil {
|
if err := cache.Context(ctx).Set(req.Key, req.Value, ttl); err != nil {
|
||||||
return errors.InternalServerError("cache.set", err.Error())
|
log.Errorf("Error writing to cache %s", err)
|
||||||
|
return errors.InternalServerError("cache.set", "Error writing to cache")
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp.Status = "ok"
|
rsp.Status = "ok"
|
||||||
@@ -60,7 +67,8 @@ func (c *Cache) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.Delet
|
|||||||
return errors.BadRequest("cache.delete", "missing key")
|
return errors.BadRequest("cache.delete", "missing key")
|
||||||
}
|
}
|
||||||
if err := cache.Context(ctx).Delete(req.Key); err != nil {
|
if err := cache.Context(ctx).Delete(req.Key); err != nil {
|
||||||
return errors.InternalServerError("cache.delete", err.Error())
|
log.Errorf("Error deleting from cache %s", err)
|
||||||
|
return errors.InternalServerError("cache.delete", "Error deleting from cache")
|
||||||
}
|
}
|
||||||
|
|
||||||
rsp.Status = "ok"
|
rsp.Status = "ok"
|
||||||
@@ -76,7 +84,8 @@ func (c *Cache) Increment(ctx context.Context, req *pb.IncrementRequest, rsp *pb
|
|||||||
// increment the value
|
// increment the value
|
||||||
v, err := cache.Context(ctx).Increment(req.Key, req.Value)
|
v, err := cache.Context(ctx).Increment(req.Key, req.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("cache.increment", err.Error())
|
log.Errorf("Error incrementing cache %s", err)
|
||||||
|
return errors.InternalServerError("cache.increment", "Error incrementing cache")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the response value
|
// set the response value
|
||||||
@@ -93,7 +102,8 @@ func (c *Cache) Decrement(ctx context.Context, req *pb.DecrementRequest, rsp *pb
|
|||||||
|
|
||||||
v, err := cache.Context(ctx).Decrement(req.Key, req.Value)
|
v, err := cache.Context(ctx).Decrement(req.Key, req.Value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.InternalServerError("cache.decrement", err.Error())
|
log.Errorf("Error decrementing cache %s", err)
|
||||||
|
return errors.InternalServerError("cache.decrement", "Error decrementing cache")
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the response value
|
// set the response value
|
||||||
|
|||||||
8
cache/proto/cache.proto
vendored
8
cache/proto/cache.proto
vendored
@@ -12,7 +12,7 @@ service Cache {
|
|||||||
rpc Decrement(DecrementRequest) returns (DecrementResponse) {}
|
rpc Decrement(DecrementRequest) returns (DecrementResponse) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get an item from the cache by key
|
// Get an item from the cache by key. If key is not found, an empty response is returned.
|
||||||
message GetRequest {
|
message GetRequest {
|
||||||
// The key to retrieve
|
// The key to retrieve
|
||||||
string key = 1;
|
string key = 1;
|
||||||
@@ -42,7 +42,7 @@ message SetResponse {
|
|||||||
string status = 1;
|
string status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete a value from the cache
|
// Delete a value from the cache. If key not found a success response is returned.
|
||||||
message DeleteRequest {
|
message DeleteRequest {
|
||||||
// The key to delete
|
// The key to delete
|
||||||
string key = 1;
|
string key = 1;
|
||||||
@@ -53,7 +53,7 @@ message DeleteResponse {
|
|||||||
string status = 1;
|
string status = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment a value (if it's a number)
|
// Increment a value (if it's a number). If key not found it is equivalent to set.
|
||||||
message IncrementRequest {
|
message IncrementRequest {
|
||||||
// The key to increment
|
// The key to increment
|
||||||
string key = 1;
|
string key = 1;
|
||||||
@@ -68,7 +68,7 @@ message IncrementResponse {
|
|||||||
int64 value = 2;
|
int64 value = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decrement a value (if it's a number)
|
// Decrement a value (if it's a number). If key not found it is equivalent to set.
|
||||||
message DecrementRequest {
|
message DecrementRequest {
|
||||||
// The key to decrement
|
// The key to decrement
|
||||||
string key = 1;
|
string key = 1;
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
curl "https://api.m3o.com/v1/event/Subscribe" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
|
||||||
-d '{
|
|
||||||
"topic": "user"
|
|
||||||
}'
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package example
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/micro/services/clients/go/event"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Subscribe to messages for a given topic.
|
|
||||||
func SubscribeToAtopic() {
|
|
||||||
eventService := event.NewEventService(os.Getenv("MICRO_API_TOKEN"))
|
|
||||||
rsp, err := eventService.Subscribe(&event.SubscribeRequest{
|
|
||||||
Topic: "user",
|
|
||||||
})
|
|
||||||
fmt.Println(rsp, err)
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
const { EventService } = require("m3o/event");
|
|
||||||
|
|
||||||
// Subscribe to messages for a given topic.
|
|
||||||
async function subscribeToAtopic() {
|
|
||||||
let eventService = new EventService(process.env.MICRO_API_TOKEN);
|
|
||||||
let rsp = await eventService.subscribe({
|
|
||||||
topic: "user",
|
|
||||||
});
|
|
||||||
console.log(rsp);
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribeToAtopic();
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
curl "https://api.m3o.com/v1/notes/Subscribe" \
|
|
||||||
-H "Content-Type: application/json" \
|
|
||||||
-H "Authorization: Bearer $MICRO_API_TOKEN" \
|
|
||||||
-d '{
|
|
||||||
"id": "63c0cdf8-2121-11ec-a881-0242e36f037a"
|
|
||||||
}'
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
package example
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/micro/services/clients/go/notes"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Specify the note to events
|
|
||||||
func SubscribeToEvents() {
|
|
||||||
notesService := notes.NewNotesService(os.Getenv("MICRO_API_TOKEN"))
|
|
||||||
rsp, err := notesService.Subscribe(¬es.SubscribeRequest{
|
|
||||||
Id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
|
||||||
})
|
|
||||||
fmt.Println(rsp, err)
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
const { NotesService } = require("m3o/notes");
|
|
||||||
|
|
||||||
// Specify the note to events
|
|
||||||
async function subscribeToEvents() {
|
|
||||||
let notesService = new NotesService(process.env.MICRO_API_TOKEN);
|
|
||||||
let rsp = await notesService.subscribe({
|
|
||||||
id: "63c0cdf8-2121-11ec-a881-0242e36f037a",
|
|
||||||
});
|
|
||||||
console.log(rsp);
|
|
||||||
}
|
|
||||||
|
|
||||||
subscribeToEvents();
|
|
||||||
Reference in New Issue
Block a user