golang操作memcached 转自https://blog.csdn.net/weixin_37696997/article/details/78760397
go使用memcached需要第三方的驱动库,这里有一个库是memcached作者亲自实现的,代码质量效率肯定会有保障
1:安装
go get github.com/bradfitz/gomemcache/memcache
1
2:使用
import "github.com/bradfitz/gomemcache/memcache"
1
3:一个栗子
package main
import (
"fmt"
"github.com/bradfitz/gomemcache/memcache"
)
var (
server = "127.0.0.1:11211"
)
func main() {
//create a handle
mc := memcache.New(server)
if mc == nil {
fmt.Println("memcache New failed")
}
//set key-value
mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})
//get key's value
it, _ := mc.Get("foo")
if string(it.Key) == "foo" {
fmt.Println("value is ", string(it.Value))
} else {
fmt.Println("Get failed")
}
///Add a new key-value
mc.Add(&memcache.Item{Key: "foo", Value: []byte("bluegogo")})
it, err := mc.Get("foo")
if err != nil {
fmt.Println("Add failed")
} else {
if string(it.Key) == "foo" {
fmt.Println("Add value is ", string(it.Value))
} else {
fmt.Println("Get failed")
}
}
//replace a key's value
mc.Replace(&memcache.Item{Key: "foo", Value: []byte("mobike")})
it, err = mc.Get("foo")
if err != nil {
fmt.Println("Replace failed")
} else {
if string(it.Key) == "foo" {
fmt.Println("Replace value is ", string(it.Value))
} else {
fmt.Println("Replace failed")
}
}
//delete an exist key
err = mc.Delete("foo")
if err != nil {
fmt.Println("Delete failed:", err.Error())
}
//incrby
err = mc.Set(&memcache.Item{Key: "aaa", Value: []byte("1")})
if err != nil {
fmt.Println("Set failed :", err.Error())
}
it, err = mc.Get("foo")
if err != nil {
fmt.Println("Get failed ", err.Error())
} else {
fmt.Println("src value is:", it.Value)
}
value, err := mc.Increment("aaa", 7)
if err != nil {
fmt.Println("Increment failed")
} else {
fmt.Println("after increment the value is :", value)
}
//decrby
value, err = mc.Decrement("aaa", 4)
if err != nil {
fmt.Println("Decrement failed", err.Error())
} else {
fmt.Println("after decrement the value is ", value)
}
}
---------------------
作者:代码界的小姑娘
来源:CSDN
原文:https://blog.csdn.net/weixin_37696997/article/details/78760397
版权声明:本文为博主原创文章,转载请附上博文链接!
golang操作memcached 转自https://blog.csdn.net/weixin_37696997/article/details/78760397的更多相关文章
- Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740
Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...
- https://blog.csdn.net/u011489043/article/details/68488459
转自https://blog.csdn.net/u011489043/article/details/68488459 String 字符串常量 StringBuffer 字符串变量(线程安全) ...
- 转-java编译时error: illegal character '\ufeff' 的解决办法-https://blog.csdn.net/t518vs20s/article/details/80833061
原文链接:https://blog.csdn.net/shixing_11/article/details/6976900 最近开发人员通过SVN提交了xxx.java文件,因发布时该包有问题需要回退 ...
- IntelliJ Idea 常用快捷键列表 (需整理下) https://blog.csdn.net/dc_726/article/details/42784275
[常规] https://blog.csdn.net/dc_726/article/details/42784275https://jingyan.baidu.com/article/59a015e3 ...
- 抽象类和接口有什么区别---https://blog.csdn.net/csdn_aiyang/article/details/71171886
https://blog.csdn.net/csdn_aiyang/article/details/71171886 概念] 抽象类.具体类是相对的,并非绝对的.抽象是一种概念性名词,具体是一种可 ...
- 为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212
为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212 2018年12月05日 14:30:19 绝圣 ...
- MVC和WebApi 使用get和post 传递参数。 转载https://blog.csdn.net/qq373591361/article/details/51508806
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq373591361/article/details/51508806我们总结一下用js请求服务器的 ...
- Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411
转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...
- 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838
ControlTemplate(控件模板) https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...
随机推荐
- Leetcode 600 不含连续1的非负整数
给定一个正整数 n,找出小于或等于 n 的非负整数中,其二进制表示不包含 连续的1 的个数. 例如: 输入: 5 输出: 5 解释: 下面是带有相应二进制表示的非负整数<= 5: 0 : 0 1 ...
- 计算1~100之间,能被3整除但是不能被7整除的数的和(C语言)
#include<stdio.h> int main(agrc *agrv) { int n,i; int sum=0; scanf("%d",&n); for ...
- 安装openssh
在推进ansible用于配置管理与自动部署,其中一个很困扰的问题是创建ssh通道很慢,虽然ansible在同一个task里面是并行的控制多台受控端.但是每一个task都需要和受控端创建ssh通道,非常 ...
- sql注入2
一.查询数据库的版本号 http://10.1.2.5:10631/sqli/Less-2/?id=-1 union select 1,2,version() 二.查询数据库中所有的库名 http:/ ...
- Qt坑点汇总
1.场景:假如我们想在layout中的qlabel中设置一个图片 1.1 如果简单地使用border-image,我们可以做到,并且拖动界面时,label可以随布局正常变化,这里需要注意的是,修改ui ...
- 【解决】Server Tomcat v7.0 Server at localhost failed to start.
Server Tomcat v7.0 Server at localhost failed to start. 出现此原因是因为servlet-name不匹配 修改一致即可
- reducer 按key聚合
#coding=utf8 import sys import itertools import json reload(sys) sys.setdefaultencoding("utf8&q ...
- 连接mysql用mysql_connect不能连接
用mysqli_connect就可以解决. mysqli_array()有连个参数,第一个是连接mysql名. php遇到这个警告Warning: Use of undefined constant ...
- 双十一LoanMarket压力测试报告
测试背景 评估服务器资源及几个重要接口的并发性能. 测试需求 由开发提供的5个重要接口: 业务场景 URL 访问量(万) TPS 请求比例(%) 随手借点-首页产品接口 /suishoujiedian ...
- fiddler 一些不为人知的功能
1. fiddler的ctrl+F查找功能 可以进行正则表达式查找: 勾选Regular Expression,find中出现REGEX:,在这后面输入正则表达式即可进行匹配查找 2. fiddler ...