分布式缓冲之memcache

1.memcache简介memcache是danga.com的一个项目,它是一款开源的高性能的分布式内存对象缓存系统,,最早是给LiveJournal提供服务的,后来逐渐被越来越多的大型网站所采用

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

1. memcache简介

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

  分布式缓冲之memcache

  为了在内存中提供数据的高速查找能力,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

分布式缓冲之memcache
分布式缓冲之memcache

/*  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;}

分布式缓冲之memcache

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120095.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 用opencv的dnn模块做yolov5目标检测[通俗易懂]

    用opencv的dnn模块做yolov5目标检测[通俗易懂]最近在微信公众号里看到多篇讲解yolov5在openvino部署做目标检测文章,但是没看到过用opencv的dnn模块做yolov5目标检测的。于是,我就想着编写一套用opencv的dnn模块做yolov5目标检测的程序。在编写这套程序时,遇到的bug和解决办法,在这篇文章里讲述一下。在yolov5之前的yolov3和yolov4的官方代码都是基于darknet框架的实现的,因此opencv的dnn模块做目标检测时,读取的是.cfg和.weight文件,那时候编写程序很顺畅,没有遇到bug。但是yolo

    2022年10月13日
    3
  • eruda.js 移动端调试神器使用教程(eruda)

    eruda.js 移动端调试神器使用教程(eruda)在日常的移动端开发时,一般都是试用chrome浏览器的移动端模式进行开发和调试,只有在chrome调试完成,没有问题了才会上到真机测试,移动端开发的一大问题就在于此,各种品牌各种型号手机,手机中各种类型的浏览器APP…还好移动端的相对一致点,但是往往都会有一些各种各样的坑,这时候就蛋疼了,明明chrome调试工具中是正常的,一到某个浏览器中就炸了,怎么办,又无法像在chrome中使用调试工具进行调试,只能通过alert()弹窗来调试,有什么办法可以像PC上那样清晰,可视化的调试呢?使用示例://#

    2025年7月23日
    2
  • HTTP和HTTPS协议,看一篇就够了

    HTTP和HTTPS协议,看一篇就够了一、前言:我们上网很简单,只需要通过网络服务商开通端口就可以了,每天都在上网,有留意到访问网站链接有什么共同点吗?为什么需要但是为什么要讲HTTP和HTTPS呢?二、HTTP和HTTPS发展历史当我们打开谷歌浏览器输入www.12306.cn,回车很快在浏览器上就看到页面,其中的浏览器就是客户端,负责接受浏览器的是服务器,两者的通信是通过HTTP协议。什么是HTTP?…

    2022年4月29日
    74
  • latex的大括号_大括号和小括号的用法

    latex的大括号_大括号和小括号的用法LaTeX

    2022年10月11日
    2
  • Jlink-10 pin 的定义(stm32使用)官方定义

    Jlink-10 pin 的定义(stm32使用)官方定义因为在网上找了好久才找到正确的接法,所以专门记载了下来,因为stm32芯片这几个功能引脚会内置上拉电阻,所以不需要再外接电阻了。转载于:https://www.cnblogs.com/dzswise/p/7523420.html…

    2022年5月28日
    27
  • pet的结构单元_三层架构

    pet的结构单元_三层架构PetShop4架构设计分析(三) petshop4.0详解之三(PetShop数据访问层之消息处理)三、PetShop数据访问层之消息处理在进行系统设计时,除了对安全、事务等问题给与足够的重视外,性能也是一个不可避免的问题所在,尤其是一个B/S结构的软件系统,必须充分地考虑访问量、数据流量、服务器负荷的问题。解决性能的瓶颈,除了对硬件系统进行升级外,软件设计的合理性尤为重要。在前

    2022年10月16日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号