分布式缓冲之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.17.tar.gz

配置、编译、安装memcache:
./configure

make

sudo make install 

3. memcache启动和关闭

(1)启动memcache服务

/use/local/bin/memcache -d -m 10 -u root -l 192.168.1.10 -p 11211 -c 32 -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.17.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
View Code
#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 = 0;
    uint32_t  flags = 0;

    memc = memcached_create(NULL);
    server = memcached_server_list_append(NULL, "127.0.0.1", 11211, &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 0;
}

原文地址:https://www.cnblogs.com/xiaobingqianrui/p/9391033.html