Skip to content

Commit

Permalink
提供了分组的装饰器实现 (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
wureny authored Jan 1, 2024
1 parent 8c6eedc commit 37fe769
Show file tree
Hide file tree
Showing 5 changed files with 992 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ require (
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/hashicorp/golang-lru/v2 v2.0.6
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU=
go.uber.org/mock v0.2.0/go.mod h1:J0y0rp9L3xiff1+ZBfKxlC1fz2+aO16tw0tsDOixfuM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
253 changes: 253 additions & 0 deletions mock_cache_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions namespace_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ecache

import (
"context"
"time"
)

type NamespaceCache struct {
C Cache
Namespace string
}

func (c *NamespaceCache) Set(ctx context.Context, key string, val any, expiration time.Duration) error {
return c.C.Set(ctx, c.Namespace+key, val, expiration)
}

func (c *NamespaceCache) SetNX(ctx context.Context, key string, val any, expiration time.Duration) (bool, error) {
return c.C.SetNX(ctx, c.Namespace+key, val, expiration)
}

func (c *NamespaceCache) GetSet(ctx context.Context, key string, val string) Value {
return c.C.GetSet(ctx, c.Namespace+key, val)
}

func (c *NamespaceCache) Delete(ctx context.Context, key ...string) (int64, error) {
if len(key) == 1 {
return c.C.Delete(ctx, c.Namespace+key[0])
}
newkey := make([]string, len(key))
for i, v := range key {
newkey[i] = c.Namespace + v
}
return c.C.Delete(ctx, newkey...)
}

func (c *NamespaceCache) LPush(ctx context.Context, key string, val ...any) (int64, error) {
return c.C.LPush(ctx, c.Namespace+key, val...)
}

func (c *NamespaceCache) LPop(ctx context.Context, key string) Value {
return c.C.LPop(ctx, c.Namespace+key)
}

func (c *NamespaceCache) SAdd(ctx context.Context, key string, members ...any) (int64, error) {
return c.C.SAdd(ctx, c.Namespace+key, members...)
}

func (c *NamespaceCache) SRem(ctx context.Context, key string, members ...any) (int64, error) {
return c.C.SRem(ctx, c.Namespace+key, members...)
}

func (c *NamespaceCache) IncrBy(ctx context.Context, key string, value int64) (int64, error) {
return c.C.IncrBy(ctx, c.Namespace+key, value)
}

func (c *NamespaceCache) DecrBy(ctx context.Context, key string, value int64) (int64, error) {
return c.C.DecrBy(ctx, c.Namespace+key, value)
}

func (c *NamespaceCache) IncrByFloat(ctx context.Context, key string, value float64) (float64, error) {
return c.C.IncrByFloat(ctx, c.Namespace+key, value)
}

func (c *NamespaceCache) Get(ctx context.Context, key string) Value {
return c.C.Get(ctx, c.Namespace+key)
}
Loading

0 comments on commit 37fe769

Please sign in to comment.