分布式缓冲之memcache
1. memcache简介
memcache是danga.com的一个项目,它是一款开源的高性能的分布式内存对象缓存系统,,最早是给LiveJournal提供服务的,后来逐渐被越来越多的大型网站所采用,用于在应用中减少对数据库的访问,提高应用的访问速度,并降低数据库的负载。

为了在内存中提供数据的高速查找能力,memcache使用key-value形式存储和访问数据,在内存中维护一张巨大的HashTable,使得对数据查询的时间复杂度降低到O(1),保证了对数据的高性能访问,内存的空间总是有限的,当内存没有更多的空间来存储新的数据时,memcache就会使用LRU(Least Recently Used)算法,将最近不常用的数据淘汰掉,以腾出空间来存放新的数据。memcache存储支持的数据个事业是灵活多样的,通过对象的序列化机制,可以将更高层抽象的对象转换为二进制数据,存储在缓存服务器中,当前端应用需要时,又可以通过二进制内容反序列化,将数据还原成原有对象。
2. memcache安装
由于memcache使用了libevent来进行高效的网络链接处理,因此在安装memcache之前,需要安装libevent
下载libevent,这里采用的是1.4.14版本的libevent
wget https://github.com/downloads/libevent/libevent/libevent-1.4.14b-stable.tar.gz 解压:
tar -xf llibevent-1.4.14b-stable.tar.gz 配置、编译、安装libevent:
./configure make sudo make install
下载memcache,并解压
wget http://www/memcache.org/files/memcache-1.4.17.tar.gz
tar -xzvf memcache-1.4..tar.gz 配置、编译、安装memcache:
./configure make sudo make install
3. memcache启动和关闭
(1)启动memcache服务
/use/local/bin/memcache -d -m -u root -l 192.168.1.10 -p -c -p /tem/memcached.pid
@ -d:表示启动一个守护进程
@ -m:指定分配给memcache的内存数量,单位为MB,这里指定的是10MB
@ -u:用户名
@ -l:ip
@ -p:port
@ -c:最大运行的并发连接数
@ -P:指定memcache的pid文件保存的位置
(2)关闭memcache服务
kill `cat /tmp/memcached.pid`
4. memcache支持读取/写入数据方式
(1)set将数据保存到缓存服务器,如果缓冲服务器存在同样的key,则替换之
(2)add将数据保存到缓存服务器,如果缓冲服务器存在同样的key,则新增失败
(3)replace将数据替换缓冲服务器中的相同的key,如果缓冲服务器中不存在同样的key,则替换失败
(4)append将数据追加到已经存在的数据后面
(5)prepend将数据追加到已经存在的数据的前面
(6)cats提供对变量的cas操作,它将保证在进行数据更新之前,数据没有被其它人更改
(7)get从缓存服务器获取数据
(8)iner对计数器进行增量操作
(9)decr对计数器进行减量操作
(10)delete将缓存服务器上的数据删除
5. memcache C/C++客户端库libmemcached
(1)下载libmemcached,下载地址:https://launchpad.net/libmemcached/+download
(2)我下载的是libmemcached-1.0.17.tar.gz
(3)解压、配置、安装
cd /usr/local
tar -vzxf libmemcached-1.0..tar.gz ./confiure
make
sudo make install
安装目录 /usr/local/include /usr/local/lib
6. libmemcached API
/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Libmemcached library
*
* Copyright (C) 2011 Data Differential, http://datadifferential.com/
* Copyright (C) 2006-2009 Brian Aker All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * The names of its contributors may not be used to endorse or
* promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/ #pragma once /* This seems to be required for older compilers @note http://stackoverflow.com/questions/8132399/how-to-printf-uint64-t */
#ifndef __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
#endif #ifdef __cplusplus
# include <tr1/cinttypes>
# include <cstddef>
# include <cstdlib>
#else
# include <inttypes.h>
# include <stddef.h>
# include <stdlib.h>
# include <stdbool.h>
#endif #include <sys/types.h> #include <libmemcached-1.0/visibility.h>
#include <libmemcached-1.0/configure.h>
#include <libmemcached-1.0/platform.h> #include <libmemcached-1.0/limits.h>
#include <libmemcached-1.0/defaults.h> #include <libmemcached-1.0/types/behavior.h>
#include <libmemcached-1.0/types/callback.h>
#include <libmemcached-1.0/types/connection.h>
#include <libmemcached-1.0/types/hash.h>
#include <libmemcached-1.0/types/return.h>
#include <libmemcached-1.0/types/server_distribution.h> #include <libmemcached-1.0/return.h> #include <libmemcached-1.0/types.h>
#include <libmemcached-1.0/callbacks.h>
#include <libmemcached-1.0/alloc.h>
#include <libmemcached-1.0/triggers.h> #include <libhashkit-1.0/hashkit.h> #include <libmemcached-1.0/struct/callback.h>
#include <libmemcached-1.0/struct/string.h>
#include <libmemcached-1.0/struct/result.h>
#include <libmemcached-1.0/struct/allocator.h>
#include <libmemcached-1.0/struct/sasl.h>
#include <libmemcached-1.0/struct/memcached.h>
#include <libmemcached-1.0/struct/server.h>
#include <libmemcached-1.0/struct/stat.h> #include <libmemcached-1.0/basic_string.h>
#include <libmemcached-1.0/error.h>
#include <libmemcached-1.0/stats.h> // Everything above this line must be in the order specified.
#include <libmemcached-1.0/allocators.h>
#include <libmemcached-1.0/analyze.h>
#include <libmemcached-1.0/auto.h>
#include <libmemcached-1.0/behavior.h>
#include <libmemcached-1.0/callback.h>
#include <libmemcached-1.0/delete.h>
#include <libmemcached-1.0/dump.h>
#include <libmemcached-1.0/encoding_key.h>
#include <libmemcached-1.0/exist.h>
#include <libmemcached-1.0/fetch.h>
#include <libmemcached-1.0/flush.h>
#include <libmemcached-1.0/flush_buffers.h>
#include <libmemcached-1.0/get.h>
#include <libmemcached-1.0/hash.h>
#include <libmemcached-1.0/options.h>
#include <libmemcached-1.0/parse.h>
#include <libmemcached-1.0/quit.h>
#include <libmemcached-1.0/result.h>
#include <libmemcached-1.0/server.h>
#include <libmemcached-1.0/server_list.h>
#include <libmemcached-1.0/storage.h>
#include <libmemcached-1.0/strerror.h>
#include <libmemcached-1.0/touch.h>
#include <libmemcached-1.0/verbosity.h>
#include <libmemcached-1.0/version.h>
#include <libmemcached-1.0/sasl.h> #include <libmemcached-1.0/deprecated_types.h> #ifdef __cplusplus
extern "C" {
#endif LIBMEMCACHED_API
void memcached_servers_reset(memcached_st *ptr); LIBMEMCACHED_API
memcached_st *memcached_create(memcached_st *ptr); LIBMEMCACHED_API
memcached_st *memcached(const char *string, size_t string_length); LIBMEMCACHED_API
void memcached_free(memcached_st *ptr); LIBMEMCACHED_API
memcached_return_t memcached_reset(memcached_st *ptr); LIBMEMCACHED_API
void memcached_reset_last_disconnected_server(memcached_st *ptr); LIBMEMCACHED_API
memcached_st *memcached_clone(memcached_st *clone, const memcached_st *ptr); LIBMEMCACHED_API
void *memcached_get_user_data(const memcached_st *ptr); LIBMEMCACHED_API
void *memcached_set_user_data(memcached_st *ptr, void *data); LIBMEMCACHED_API
memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source); LIBMEMCACHED_API
memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key); LIBMEMCACHED_API
uint32_t memcached_server_count(const memcached_st *); LIBMEMCACHED_API
uint64_t memcached_query_id(const memcached_st *); #ifdef __cplusplus
} // extern "C"
#endif
#include "stdio.h"
#include <string>
#include <iostream>
using namespace std; #include <libmemcached/memcached.h> int main()
{
memcached_st *memc;
memcached_return rc;
memcached_server_st *server;
time_t expiration = ;
uint32_t flags = ; memc = memcached_create(NULL);
server = memcached_server_list_append(NULL, "127.0.0.1", , &rc);
rc = memcached_server_push(memc, server);
memcached_server_list_free(server); string key = "key";
string value = "value";
size_t value_length = value.length();
size_t key_length = key.length(); //Save data
rc = memcached_set(memc, key.c_str(), key.length(), value.c_str(), value.length(), expiration, flags);
cout << rc << endl;
if (rc == MEMCACHED_SUCCESS)
{
cout << "Save data:" << value << " sucessful!" << endl;
} //Get data
char* result = memcached_get(memc, key.c_str(), key_length, &value_length, &flags, &rc);
if (rc == MEMCACHED_SUCCESS)
{
cout << "Get value:" << result << " sucessful!" << endl;
} //Delete data
rc = memcached_delete(memc, key.c_str(), key_length, expiration);
if (rc == MEMCACHED_SUCCESS)
{
cout << "Delete key:" << key << " sucessful!" << endl;
} //free
memcached_free(memc);
return ;
}

分布式缓冲之memcache的更多相关文章
- 分布式缓存之 memcache 实现分布式缓存
最近想搞点分布式,但是不知道整点什么,来点简单的吧. 今天讲下memcache的分布式缓存 首先下载memcache的服务器端 百度下可以找到 然后执行安装和开启(关闭服务器)命令(还有其他的命令 可 ...
- memcache分布式实现、memcache分布…
Memcache的分布式介绍 memcached虽然称为"分布式"缓存服务器,但服务器端并没有"分布式"功能.服务器端仅包括内存存储功能,其实现非常简单.至于m ...
- 分布式缓存之Memcache
〇.为什么要用分布式缓存 1.软件从单机到分布式 走向分布式第一步就是解决:多台机器共享登录信息的问题. 例如:现在有三台机器组成了一个Web的应用集群,其中一台机器用户登录,然后其他另外两台机器共享 ...
- 分布式缓存系统——memcache
一.简介 memcache是一个自由开源的.高性能的.分布式内存对象缓存系统.它是一种基于内存的key-value存储,用来存储小块的任意数据(字符串.对象).这些数据可以是数据库调用.API调用等. ...
- memcache 分布式缓存
转载地址:http://www.cnblogs.com/phpstudy2015-6/p/6713164.html 作者:那一叶随风 1.memcached分布式简介 memcached虽然称为“分布 ...
- Memcache知识点梳理
Memcache知识点梳理 Memcached概念: Memcached是一个免费开源的,高性能的,具有分布式对象的缓存系统,它可以用来保存一些经常存取的对象或数据,保存的数据像一张巨大的HAS ...
- memcache 的内存管理介绍和 php实现memcache一致性哈希分布式算法
1 网络IO模型 安装memcached需要先安装libevent Memcached是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描 ...
- 分布式缓存(Cache)
1. 单层分布式cache. 如memcache. 2. 多层分布式cache. 服务端和调用者本地都存放cache, 使用udp组播解决cache同步更新问题,但不可靠. 3. 改进的多层分布式ca ...
- memcached分布式缓存
1.memcached分布式简介 memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能.Memcache集群主机不能够相互通信传输数据,它的“分布式”是基于客户端的程序逻辑算 ...
随机推荐
- iOS导入高德地图出现缺失armv7--"Undefined symbols for architecture armv7"
在已有项目中使用pod导入高德地图,报了以下错误: ld: warning: directory not found for option '-L/Users/paul/iOS/yun-hui-yi/ ...
- Generative model 和Discriminative model
学习音乐自动标注过程中设计了有关分类型模型和生成型模型的东西,特地查了相关资料,在这里汇总. http://blog.sina.com.cn/s/blog_a18c98e50101058u.html ...
- netty5----心跳
netty3心跳: package com.heart; import java.net.InetSocketAddress; import java.util.concurrent.Executor ...
- Linux 初始化之 Systemd机制
systemd是Linux下的一种init软件,由Lennart Poettering带头开发,其开发目标是提供更优秀的框架以表示系统服务间的依赖关系,并依此实现系统初始化时服务的并行启动,同时达到降 ...
- sqlserver索引的原理及索引建立的注意事项小结
聚集索引,数据实际上是按顺序存储的,数据页就在索引页上.就好像参考手册将所有主题按顺序编排一样.一旦找到了所要搜索的数据,就完成了这次搜索,对于非聚集索引,索引是安全独立于数据本身结构的,在索引中找到 ...
- 使用KNN算法手写体识别
#!/usr/bin/python #coding:utf-8 import numpy as np import operator import matplotlib import matplotl ...
- Maven的Mirror和Repository
今天新公司入职,项目经理让迁出项目,心想maven的阿里镜像源挺快的,干脆在配置了公司私服之后自己配置了阿里的镜像源,没成想项目屡屡报错,找不到项目依赖的公司jar包,后来才发现,同事配置mirror ...
- Nginx 自定义404、500错误页面跳转
自定义Nginx错误界面跳转 1.开启Nginx.conf配置文件下的自定义接口参数. http { fastcgi_intercept_errors on; } 2.在Server区域添加自定义的错 ...
- Refseq,accssion #,gi ,Ensembl的关系
accession编号的分子类型代号: Ensembl是2000年就开始开发的基因组自动注释软件,起初是只对真核生物基因组,2009年后开始对植物,细菌等开放.既然要注释,就要有注释对象(基因,转录本 ...
- 新机git及github sshkey简单配置
新机git简单配置,毕竟不常用,不用每次都查1.安装gitwindows:https://git-scm.com/download/winubuntu: apt install git 2.全局配置 ...