- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想在go lang中模拟memcache缓存数据来避免授权 我尝试使用 gomock 但无法解决,因为我没有任何接口(interface)。
func getAccessTokenFromCache(accessToken string)
func TestSendData(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockObj := mock_utils.NewMockCacheInterface(mockCtrl)
mockObj.EXPECT().GetAccessToken("abcd")
var jsonStr = []byte(`{
"devices": [
{"id": "avccc",
"data":"abcd/"
}
]
}`)
req, err := http.NewRequest("POST", "/send/v1/data",
bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "d958372f5039e28")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(SendData)
handler.ServeHTTP(rr, req)
if status := rr.Code; status != 200 {
t.Errorf("handler returned wrong status code: got %v want %v",
status, http.StatusOK)
}
expected := `{"error":"Invalid access token"}`
body, _ := ioutil.ReadAll(rr.Body)
if string(body) != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
string(body), expected)
}
func SendData(w http.ResponseWriter, r *http.Request) {
accessToken := r.Header.Get(constants.AUTHORIZATION_HEADER_KEY)
t := utils.CacheType{At1: accessToken}
a := utils.CacheInterface(t)
isAccessTokenValid := utils.CacheInterface.GetAccessToken(a, accessToken)
if !isAccessTokenValid {
RespondError(w, http.StatusUnauthorized, "Invalid access token")
return
}
response := make(map[string]string, 1)
response["message"] = "success"
RespondJSON(w, http.StatusOK, response)
}
尝试使用 gomock 进行模拟
package mock_utils
gen mock for utils for get access controller(1) 定义一个你想模拟的接口(interface)。
(2) 使用mockgen 从界面生成mock。(3) 在测试中使用模拟:
最佳答案
您需要构建您的代码,以便每次此类服务访问都通过接口(interface)实现进行。在您的情况下,理想情况下您应该创建一个界面,如
type CacheInterface interface {
Set(key string, val interface{}) error
Get(key string) (interface{},error)
}
您的 MemcacheStruct 应该实现此接口(interface),并且所有与内存缓存相关的调用都应该从那里发生。就像您的情况一样,GetAccessToken
应该调用 cacheInterface.get(key)
,其中您的 cacheInterface 应该引用此接口(interface)的内存缓存实现。这是设计您的 go 程序的更好方法,这不仅可以帮助您编写测试,而且在假设您想使用不同的内存数据库来帮助缓存的情况下也有帮助。例如,假设将来您想要使用 Redis 作为您的缓存存储,那么您需要更改的就是创建该接口(interface)的新实现。
关于go - 如何在 golang 中模拟 memcached 进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49688682/
memcached 能否充分利用多核?或者有什么办法可以调整这个? 最佳答案 默认情况下,memcached 是多线程的,并且在使许多内核饱和时没有问题。在更大规模的并行机器(例如 256 核 CMT
有没有办法根据通配符键使内存缓存中的条目无效? 因此,如果我有以下内存缓存键: data/1 data/2 data/3 有没有一种方法可以使这些 key 失效,例如 data/* ?一口气清除一堆陈
Memcache 可以配置为使用磁盘存储而不是 RAM 吗? 我在 High IO Amazon EC2 实例上运行 Memcache Server。该实例有 2TB 的 SSD 存储可用。我可以将
memcached 的最大 key 过期时间是多少? 如果我不提供过期时间并且缓存已满,会发生什么? 最佳答案 您可以通过提供 Unix 时间戳而不是天数来将 key 过期设置为某个日期。该日期可以是
我有一个运行 php/mysql 的网站。该应用程序将被多次“复制”。每个实例都会获得自己的数据库、一组 php 文件(通配符)域,您明白了。 现在应用程序的某些部分是使用 memcache 进行缓存
是否可以根据某些正则表达式获取 memcached key 列表?我知道一种解决方案是将 key 存储在数据库中,并在需要删除这些 key 时获取列表。这意味着,它将给数据库带来额外的成本。 我想知道
我们发现使用我们的 memcache 时,一些键往往会丢失。为了调试,我使用命令 stats cachedump 输出是这样的: ITEM key-stuff-123-mlgtr-tmura [5
从 Network Solutions 移至托管站点,出现错误 fatal error :第 6 行 上/data/.../include/connect.inc 中未找到类“Memcache” 我该
我遵循了这个 tutorial 中的说明关于如何安装 nginx、php 和 mysql,包括 php5-memcache。 我想知道我是否还需要安装没有“php5”前缀的 memcache 或 me
我在 ubuntu 12.04 上运行 apache/php/memcached 我设置的 key 在过期后仍然有效。我们使用的代码与我们从中迁移的内存缓存 PAAS 的代码相同。 我如何确认过期实际
假设我在开始时有两个 memcached 节点( 节点 A, B ),当我添加一个新的 时节点 C ,部分键被重新映射,并且由于一致散列只有其中一些。 让我们假设一个键为“ foo ”的值原来在服务器
真的,我只想知道memcached中的slab是什么。如果有认识的同事能回答我的话会更好。 谢谢你的回答... 最佳答案 长时间运行的应用程序(如 memcached)会遇到内存碎片问题,服务运行时间
在 memcached 概述中 here 它说: Memcached servers are generally unaware of each other. There is no crosstal
当您在 memcached 中放入一个值时,您可以设置一个过期时间。 但是,当您增加一个值时,您不能(至少不是来自 PHP )设置新的到期时间。 我的问题:到期时间是否在递增时重置为其初始值?或者它没
我在我的 centos 服务器上使用 memcached,我的项目很大并且有超过 1MB 的对象我需要保存到 memcached,好吧,我不能!因为 max_item_size 是 1MB,无论如何要
我尝试使用本教程在 MAMP - Snow Leopard 10.6.2 中安装 memcached: Setup a Memcached-Enabled MAMP Sandbox Environme
我试图添加的方式 -l 11211 -l 11212 在 memcached conf 文件中。但它只是在听第一个,即 1121 最佳答案 首先我使用了 mikewied 的解决方案,但后来我遇到了自
我希望记录我的 memcached 服务器中正在进行的所有事件。所有读取和写入。 这将用作云中许多远程 php 应用程序的分布式守护程序,并且需要一种通过 SSH 登录并检查守护程序上正在进行的事件的
我在使用 memcached 时遇到了一些问题,我的一个想法是它可能已经在我尝试运行它的端口上运行,由我们网络上的其他用户启动。有没有办法知道当前正在使用哪些 memcached 端口? 最佳答案 要
运行 4 个 .5GB 内存缓存服务器或运行一个 2GB 实例有什么区别吗? 运行多个实例有什么好处吗? 最佳答案 如果一个实例失败,您仍然可以享受使用缓存的优势。如果您使用一致性哈希将相同的数据引入
我是一名优秀的程序员,十分优秀!