代码拉取完成,页面将自动刷新
同步操作将从 FishGoddess/cachego 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// Copyright 2020 FishGoddess. All Rights Reserved.
//
// 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 cachego
import (
"context"
"testing"
"time"
)
// go test -cover -run=^TestCache$
func TestCache(t *testing.T) {
cache := NewCache()
key := "key"
value := 123
cache.Set(key, value)
if v, err := cache.Get(key); IsNotFound(err) || v != value {
t.Error("Before reset cache, cache.Of(key) returns wrong err or value!")
}
cache.DeleteAll()
if _, err := cache.Get(key); !IsNotFound(err) || cache.Size() != 0 {
t.Error("Cache should be reset!")
}
cache.Set(key, value)
if v, err := cache.Get(key); IsNotFound(err) || v != value {
t.Error("Before delete key, cache.Of(key) returns wrong err or value!")
}
cache.Delete(key)
if _, err := cache.Get(key); !IsNotFound(err) {
t.Error("After deleting key, key should be dead!")
}
}
// go test -cover -run=^TestCacheTTL$
func TestCacheTTL(t *testing.T) {
cache := NewCache()
cache.AutoGC(3 * time.Second)
key := "key"
value := 123
cache.Set(key, value, WithOpTTL(1*time.Second))
if v, err := cache.Get(key); IsNotFound(err) || cache.Size() != 1 || v != value {
t.Error("Before ttl, returns wrong err or size or value!")
}
time.Sleep(2 * time.Second)
if _, err := cache.Get(key); !IsNotFound(err) {
t.Error("After ttl, key should be dead!")
}
if cache.Size() != 1 {
t.Error("After ttl, size should be 1!")
}
time.Sleep(2 * time.Second)
if cache.Size() != 0 {
t.Error("After gc, size should be 0!")
}
}
// go test -cover -run=^TestGetWithLoad$
func TestGetWithLoad(t *testing.T) {
cache := NewCache()
loadFunc := func(ctx context.Context) (data interface{}, err error) {
return "loadFunc", nil
}
key := "key"
value := "get"
cache.Set(key, value, WithOpTTL(1*time.Second))
if v, err := cache.Get(key, WithOpOnMissed(loadFunc), WithOpTTL(time.Second)); err != nil || v.(string) != value {
t.Errorf("Before Sleep, cache.Of(key) returns err %+v or wrong value %s!", err, v.(string))
}
time.Sleep(2 * time.Second)
if v, err := cache.Get(key, WithOpOnMissed(loadFunc), WithOpTTL(time.Second)); err != nil || v.(string) != "loadFunc" {
t.Errorf("After Sleep, cache.Of(key) returns err %+v or wrong value %s!", err, v.(string))
}
}
// go test -cover -run=^TestCacheAutoGC$
func TestCacheAutoGC(t *testing.T) {
cache := NewCache()
cache.AutoGC(2 * time.Second) <- struct{}{}
key := "key"
value := 123
cache.Set(key, value, WithOpTTL(1*time.Second))
if v, err := cache.Get(key); IsNotFound(err) || cache.Size() != 1 || v != value {
t.Error("Before gc, returns wrong err or size or value!")
}
time.Sleep(3 * time.Second)
if _, err := cache.Get(key); !IsNotFound(err) || cache.Size() != 1 {
t.Error("After gc, key should be dead!")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。