1
0
Fork 0

Adding upstream version 1.0.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-18 14:17:55 +02:00
parent bc73131a12
commit 582ebf4048
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
14 changed files with 1083 additions and 0 deletions

112
memcache/memcache.go Normal file
View file

@ -0,0 +1,112 @@
// Copyright 2013 Beego Authors
// Copyright 2014 The Macaron Authors
// Copyright 2024 The Forgejo Authors
//
// 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 memcache
import (
"fmt"
"strings"
"github.com/bradfitz/gomemcache/memcache"
"code.forgejo.org/go-chi/cache"
)
// Cacher represents a memcache cache adapter implementation.
type Cacher struct {
c *memcache.Client
}
func NewItem(key string, data []byte, expire int32) *memcache.Item {
return &memcache.Item{
Key: key,
Value: data,
Expiration: expire,
}
}
// Put puts value into cache with key and expire time.
// If expired is 0, it lives forever.
func (c *Cacher) Put(key string, val any, expire int64) error {
var v []byte
switch val := val.(type) {
case string:
v = []byte(val)
case []byte:
v = val
default:
// Best effort.
v = []byte(fmt.Sprint(val))
}
return c.c.Set(NewItem(key, v, int32(expire)))
}
// Get gets cached value by given key.
func (c *Cacher) Get(key string) any {
item, err := c.c.Get(key)
if err != nil {
return nil
}
return string(item.Value)
}
// Delete deletes cached value by given key.
func (c *Cacher) Delete(key string) error {
if err := c.c.Delete(key); err != nil && err != memcache.ErrCacheMiss {
return err
}
return nil
}
// Incr increases cached int-type value by given key as a counter.
func (c *Cacher) Incr(key string) error {
_, err := c.c.Increment(key, 1)
return err
}
// Decr decreases cached int-type value by given key as a counter.
func (c *Cacher) Decr(key string) error {
_, err := c.c.Decrement(key, 1)
return err
}
// IsExist returns true if cached value exists.
func (c *Cacher) IsExist(key string) bool {
_, err := c.c.Get(key)
return err == nil
}
// Flush deletes all cached data.
func (c *Cacher) Flush() error {
return c.c.FlushAll()
}
// StartAndGC starts GC routine based on config string settings.
// AdapterConfig: 127.0.0.1:9090;127.0.0.1:9091
func (c *Cacher) StartAndGC(opt cache.Options) error {
c.c = memcache.New(strings.Split(opt.AdapterConfig, ";")...)
return nil
}
// Ping tests if the cache is alive.
func (c *Cacher) Ping() error {
return cache.GenericPing(c)
}
func init() {
cache.Register("memcache", &Cacher{})
}

116
memcache/memcache_test.go Normal file
View file

@ -0,0 +1,116 @@
// Copyright 2014 The Macaron Authors
//
// 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 memcache
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"code.forgejo.org/go-chi/cache"
)
func Test_MemcacheCacher(t *testing.T) {
conn := os.Getenv("MEMCACHE_CONN")
if conn == "" {
conn = "127.0.0.1:9090"
}
opt := cache.Options{
Adapter: "memcache",
AdapterConfig: conn,
}
t.Run("Basic operations", func(t *testing.T) {
c, err := cache.NewCacher(opt)
require.NoError(t, err)
assert.NotNil(t, c)
assert.NoError(t, c.Put("uname", "some-user-name", 1))
assert.NoError(t, c.Put("uname2", "unknwon2", 1))
assert.True(t, c.IsExist("uname"))
assert.Nil(t, c.Get("404"))
assert.EqualValues(t, "some-user-name", c.Get("uname"))
time.Sleep(1 * time.Second)
assert.Nil(t, c.Get("uname"))
time.Sleep(1 * time.Second)
assert.Nil(t, c.Get("uname2"))
require.NoError(t, c.Put("uname", "some-user-name", 0))
require.NoError(t, c.Delete("uname"))
assert.Nil(t, c.Get("uname"))
require.NoError(t, c.Put("uname", "some-user-name", 0))
require.NoError(t, c.Flush())
assert.Nil(t, c.Get("uname"))
require.NoError(t, c.Delete("uname"))
})
t.Run("Increase and decrease operations", func(t *testing.T) {
c, err := cache.NewCacher(opt)
require.NoError(t, err)
assert.NotNil(t, c)
require.Error(t, c.Incr("404"))
require.Error(t, c.Decr("404"))
require.NoError(t, c.Put("int", 0, 0))
require.NoError(t, c.Put("int32", int32(0), 0))
require.NoError(t, c.Put("int64", int64(0), 0))
require.NoError(t, c.Put("uint", uint(0), 0))
require.NoError(t, c.Put("uint32", uint32(0), 0))
require.NoError(t, c.Put("uint64", uint64(0), 0))
require.NoError(t, c.Put("string", "hi", 0))
require.NoError(t, c.Incr("int"))
require.NoError(t, c.Incr("int32"))
require.NoError(t, c.Incr("int64"))
require.NoError(t, c.Incr("uint"))
require.NoError(t, c.Incr("uint32"))
require.NoError(t, c.Incr("uint64"))
assert.EqualValues(t, "1", c.Get("int"))
assert.EqualValues(t, "1", c.Get("int32"))
assert.EqualValues(t, "1", c.Get("int64"))
assert.EqualValues(t, "1", c.Get("uint"))
assert.EqualValues(t, "1", c.Get("uint32"))
assert.EqualValues(t, "1", c.Get("uint64"))
require.NoError(t, c.Decr("int"))
require.NoError(t, c.Decr("int32"))
require.NoError(t, c.Decr("int64"))
require.NoError(t, c.Decr("uint"))
require.NoError(t, c.Decr("uint32"))
require.NoError(t, c.Decr("uint64"))
require.Error(t, c.Incr("string"))
require.Error(t, c.Decr("string"))
assert.EqualValues(t, "0", c.Get("int"))
assert.EqualValues(t, "0", c.Get("int32"))
assert.EqualValues(t, "0", c.Get("int64"))
assert.EqualValues(t, "0", c.Get("uint"))
assert.EqualValues(t, "0", c.Get("uint32"))
assert.EqualValues(t, "0", c.Get("uint64"))
require.NoError(t, c.Flush())
})
}