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包含数据模板和控件模板,其中 ...
随机推荐
- makefile笔记4 - makefile命令
每条规则中的命令和操作系统 Shell 的命令行是一致的. make 会一按顺序一条一条的执行命令,每条命令的开头必须以[Tab]键开头,除非,命令是紧跟在依赖规则后面的分号后的. 在命令行之间中的空 ...
- .NET复习笔记-泛型
1.yield关键字用于返回迭代器具体的值,如下框代码所示 /// 返回0~9整数集合 public static IEnumerable<int> yieldSampleMethod() ...
- JAVA作业三
(一)学习总结 1.阅读下面程序,分析是否能编译通过?如果不能,说明原因.应该如何修改?程序的运行结果是什么?为什么子类的构造方法在运行之前,必须调用父 类的构造方法?能不能反过来? class Gr ...
- 将img设置成div背景图片
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Python 内编写类的各种技巧和方法
Python 内编写类的各种技巧和方法 简介 有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象 ...
- Linux 驱动——Led驱动2
led_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/init ...
- Spring AOP选择切点的问题
先上代码: /** * 管理员登录方法的切入点 */ @Pointcut("execution(* com.arch.shiro.realm.UserRealm.doGetAuthentic ...
- iBATIS typeHandler selectKey
typeHandler 是针对把数据库里面的某列的数据类型转换的应用程序中的数据类型,简单的说就是把 type=>dbType 反之把dbType=>type. 例如数据库某列的内容是l ...
- Jenkins强制语言设置
最近对照各种Jenkins的教程,发现在jenkins的页面中经常会中英文混合,每个人根据各自系统的不同也会出现语言不同,虽然可以翻译过来,但是中英文的混合差异还是蛮大的,造成项目间沟通障碍. 所以, ...
- Js/Session和Cookies的区别
1.cookies数据存放在客户的浏览器上面,session放在服务器上面.2.cookies不安全,别人可以分析浏览器的数据进行cookies的欺骗,考虑到安全性,应该使用cookie3.sessi ...