mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-23 07:41:25 +00:00
add 1mb value limit to cache
This commit is contained in:
2
cache/README.md
vendored
2
cache/README.md
vendored
@@ -3,3 +3,5 @@ Simple key-value storage
|
|||||||
# Cache Service
|
# Cache Service
|
||||||
|
|
||||||
The cache service provides simple get/set/delete key-value storage along with ttl support.
|
The cache service provides simple get/set/delete key-value storage along with ttl support.
|
||||||
|
|
||||||
|
The max value size is 1mb
|
||||||
|
|||||||
13
cache/handler/cache.go
vendored
13
cache/handler/cache.go
vendored
@@ -31,7 +31,12 @@ func (c *Cache) Get(ctx context.Context, req *pb.GetRequest, rsp *pb.GetResponse
|
|||||||
|
|
||||||
func (c *Cache) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse) error {
|
func (c *Cache) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse) error {
|
||||||
if len(req.Key) == 0 {
|
if len(req.Key) == 0 {
|
||||||
return errors.BadRequest("cache.get", "missing key")
|
return errors.BadRequest("cache.set", "missing key")
|
||||||
|
}
|
||||||
|
|
||||||
|
// max size 1mb e.g byte * 1024 * 1024
|
||||||
|
if len(req.Value) > 1e6 {
|
||||||
|
return errors.BadRequest("cache.set", "value is too big")
|
||||||
}
|
}
|
||||||
|
|
||||||
ttl := time.Time{}
|
ttl := time.Time{}
|
||||||
@@ -51,7 +56,7 @@ func (c *Cache) Set(ctx context.Context, req *pb.SetRequest, rsp *pb.SetResponse
|
|||||||
|
|
||||||
func (c *Cache) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
func (c *Cache) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.DeleteResponse) error {
|
||||||
if len(req.Key) == 0 {
|
if len(req.Key) == 0 {
|
||||||
return errors.BadRequest("cache.get", "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())
|
return errors.InternalServerError("cache.delete", err.Error())
|
||||||
@@ -64,7 +69,7 @@ func (c *Cache) Delete(ctx context.Context, req *pb.DeleteRequest, rsp *pb.Delet
|
|||||||
|
|
||||||
func (c *Cache) Increment(ctx context.Context, req *pb.IncrementRequest, rsp *pb.IncrementResponse) error {
|
func (c *Cache) Increment(ctx context.Context, req *pb.IncrementRequest, rsp *pb.IncrementResponse) error {
|
||||||
if len(req.Key) == 0 {
|
if len(req.Key) == 0 {
|
||||||
return errors.BadRequest("cache.get", "missing key")
|
return errors.BadRequest("cache.increment", "missing key")
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment the value
|
// increment the value
|
||||||
@@ -82,7 +87,7 @@ func (c *Cache) Increment(ctx context.Context, req *pb.IncrementRequest, rsp *pb
|
|||||||
|
|
||||||
func (c *Cache) Decrement(ctx context.Context, req *pb.DecrementRequest, rsp *pb.DecrementResponse) error {
|
func (c *Cache) Decrement(ctx context.Context, req *pb.DecrementRequest, rsp *pb.DecrementResponse) error {
|
||||||
if len(req.Key) == 0 {
|
if len(req.Key) == 0 {
|
||||||
return errors.BadRequest("cache.get", "missing key")
|
return errors.BadRequest("cache.decrement", "missing key")
|
||||||
}
|
}
|
||||||
|
|
||||||
v, err := cache.Context(ctx).Decrement(req.Key, req.Value)
|
v, err := cache.Context(ctx).Decrement(req.Key, req.Value)
|
||||||
|
|||||||
Reference in New Issue
Block a user