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包含数据模板和控件模板,其中 ...
随机推荐
- OpenGL中VA,VAO,VBO和EBO的区别
1,顶点数组(Vertex Array) VA,顶点数组也是收集好所有的顶点,一次性发送给GPU.不过数据不是存储于GPU中的,绘制速度上没有显示列表快,优点是可以修改数据. 4.VBO(Vertex ...
- [Hive安装问题]
启动Hive时出现: Exception in thread "main" java.lang.RuntimeException: java.lang.IllegalArgumen ...
- 2017-9-3模拟赛T3 密码(key)
题目 题解 这题用类似暴力+优化(划掉)的思想. 对于每个轨迹串,求出每一位向后的第一个0-9间某个数字的位置(如123112中3后面第1个2的位置为从左往右数第6个),复杂度O(Σn)=O(L). ...
- Javascript学习--BOM操作
1 获取UA(user Agent)用户代理 <!DOCtype html> <html> <head> <title></title> & ...
- windows与sql身份登录
windows身份验证:1.“.”代表本地2.127.0.0.1代表本地3.本地ip地址,在dos中可查4.localhost代表本地 sql身份验证:首先用windows登上去,之后再安全性中找到登 ...
- C语言---指针变量详解1
数据在内存中的地址也称为指针,如果一个变量存储了一份数据的指针,我们就称它为指针变量.在C语言中,允许用一个变量来存放指针,这种变量称为指针变量.指针变量的值就是某份数据的地址,这样的一份数据可以是数 ...
- Spring 内部注入bean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- React事件绑定与解绑
React中事件分类 React中事件绑定分为两种: 1.直接添加在React元素上的事件,这是React在基于Virtual DOM的基础上实现的符合w3c规范的合成事件(SyntheticEven ...
- 为UITextField增加MaxLength特性
iOS 实现方案 在 HTML 的世界里,输入框天生就有 MaxLength 属性,可以限制用户输入的最大字符数量 可惜 iOS 上对应的 UITextField 并没有这样方便的属性,只有自己动手来 ...
- Flyweight 享元(结构型)
一:描述:(该模式实际应用较少) Flyweight 享元模式是对大量细粒度的元素进行共享和重用.减少对象的创建减轻内存: 注和单例模式不同的是:享元模式的各个对象佣有各自的行为并可实例化,单例模式的 ...