memcache是danga.com的一個項目,它是一款開源的高性能的分佈式內存對象緩存系統,,最先是給LiveJournal提供服務的,後來逐漸被愈來愈多的大型網站所採用,用於在應用中減小對數據庫的訪問,提升應用的訪問速度,並下降數據庫的負載。前端
爲了在內存中提供數據的高速查找能力,memcache使用key-value形式存儲和訪問數據,在內存中維護一張巨大的HashTable,使得對數據查詢的時間複雜度下降到O(1),保證了對數據的高性能訪問,內存的空間老是有限的,當內存沒有更多的空間來存儲新的數據時,memcache就會使用LRU(Least Recently Used)算法,將最近不經常使用的數據淘汰掉,以騰出空間來存放新的數據。memcache存儲支持的數據個事業是靈活多樣的,經過對象的序列化機制,能夠將更高層抽象的對象轉換爲二進制數據,存儲在緩存服務器中,當前端應用須要時,又能夠經過二進制內容反序列化,將數據還原成原有對象。ios
因爲memcache使用了libevent來進行高效的網絡連接處理,所以在安裝memcache以前,須要安裝libeventgit
下載libevent,這裏採用的是1.4.14版本的libeventgithub
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
(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:表示啓動一個守護進程vim
@ -m:指定分配給memcache的內存數量,單位爲MB,這裏指定的是10MB緩存
@ -u:用戶名服務器
@ -l:ip網絡
@ -p:port
@ -c:最大運行的併發鏈接數
@ -P:指定memcache的pid文件保存的位置
(2)關閉memcache服務
kill `cat /tmp/memcached.pid`
(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將緩存服務器上的數據刪除
(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
/* 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 = 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; }