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的更多相关文章

  1. 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 ...

  2. https://blog.csdn.net/u011489043/article/details/68488459

    转自https://blog.csdn.net/u011489043/article/details/68488459 String 字符串常量   StringBuffer 字符串变量(线程安全) ...

  3. 转-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文件,因发布时该包有问题需要回退 ...

  4. 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 ...

  5. 抽象类和接口有什么区别---https://blog.csdn.net/csdn_aiyang/article/details/71171886

    https://blog.csdn.net/csdn_aiyang/article/details/71171886 概念]   抽象类.具体类是相对的,并非绝对的.抽象是一种概念性名词,具体是一种可 ...

  6. 为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212

    为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212 2018年12月05日 14:30:19 绝圣 ...

  7. MVC和WebApi 使用get和post 传递参数。 转载https://blog.csdn.net/qq373591361/article/details/51508806

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq373591361/article/details/51508806我们总结一下用js请求服务器的 ...

  8. Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411

    转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...

  9. 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838

    ControlTemplate(控件模板)   https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...

随机推荐

  1. hadoop题目(一)

    一.简要描述如何安装配置一个开源hadoop,只描述即可,列出完整步骤. 答:①创建一个用户和用户组,用来管理hadoop项目:   ②修改确定IP地址:vim /etc/sysconfig/netw ...

  2. pytest自动化5:pytest-html插件--生成html报告

    前言:pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.  pip 安装: pip install pytes ...

  3. elasticsearch(3) 数据操作-更新

    一 更新整个文档 更新整个文档的方法和存放数据的方式是相同的,通过PUT 127.0.0.1/test/test/1  我们可以把test/test/1下的文档更新为新的文档 例: PUT 127.0 ...

  4. Fasttext原理

    fastText 模型输入一个词的序列(一段文本或者一句话),输出这个词序列属于不同类别的概率.序列中的词和词组组成特征向量,特征向量通过线性变换映射到中间层,中间层再映射到标签.fastText 在 ...

  5. spring cloud 的安全连接

    Spring Cloud可以增加HTTP Basic认证来增加服务连接的安全性. 1.pom.xml加入security启动器 <dependency> <!-- Spring Cl ...

  6. 洛谷题解 P1315 【观光公交】

    这道题很多人都用的模拟(或者暴力),今天我就写一个"标准"的贪心发给大家.(我这段代码差点超时···也差点超内存···) 主要思路:通过贪心求得最小值即可,把加速器用到乘客最多的两 ...

  7. 'touch' 不是内部或外部命令,也不是可运行的程序或批处理文件。

    touch是Linux环境下的命令,当我们在cmd中使用时会弹出以下问题 在cmd中我们可以使用echo test> 然后我们用dir命令来查看一下当前文件夹下文件有没有创建 文件成功创建.

  8. Centos中MySQL数据的备份和恢复

    1.MySQL数据备份 MySQL在Centos备份中用到了mysqldump这个文件,首先得把它找出来: [root@instance-3snz20bz ~]# whereis mysqldump ...

  9. vue+koa实现简单的图书小程序(2)

    记录一下实现我们图书的扫码功能: https://developers.weixin.qq.com/miniprogram/dev/api/scancode.html要多读文档 scanBook () ...

  10. ---————for循环打印爱心

    //打印爱心public class Xin{ public static void main (String [] args){ for(int i=1;i<=4;i++){ for(int ...