• 将MySQL数据映射到Memcached


    将MySQL数据映射到Memcached

    2月 26th, 2008

    作者:张立冰
    出处:http://www.libing.name/2008/02/26/mysql-map-memcached.html

    本文介绍利用MySQL UDFS For Memcached与MySQL触发程序结合实现将MySQL的数据映射到Memcached的一种方法,能实现当MySQL数据更新时,实时更新Memcache数据的效果,从而减少开发相应程序来维护Memcached数据。

    MySQL UDFS For Memcached的官方介绍:

    This is a set of MySQL UDFs (user defined functions) to work with memcached using libmemcached. With these functions you get, set, append, prepend, delete, increment, decrement objects in memcached, as well as set which servers to use and which behavior the server connections will use. Combine these functions with MySQL triggers and you can manage your memcached cache. The library makes use of libmemcached. You can also use memcached as a global sequence generator for MySQL by making use of the increment function. These functions are compatible with all versions of MySQL.


    在测试之间需要安装以下软件及开发包:

    MySQL 5.0+
    Memcached (libevent…)
    Libmemcached

    下载UDFs:http://download.tangent.org/memcached_functions_mysql-0.2.tar.gz

    编译安装:

    tar xzvf memcached_functions_mysql-0.2.tar.gz
    cd memcached_functions_mysql-0.2
    ./configure –with-mysql=MYSQL_DIR/bin/mysql_config –libdir=MYSQL_DIR/lib/mysql/
    make
    make install

    安装完成后将UDFs加载到MySQL中:

    memc_servers_set()
    mysql> create function memc_servers_set returns int soname “libmemcached_functions_mysql.so”;

    memc_set()
    mysql> create function memc_set returns int soname “libmemcached_functions_mysql.so”;

    memc_get()
    mysql> create function memc_get returns string soname “libmemcached_functions_mysql.so”;

    memc_delete()
    mysql> create function memc_delete returns string soname “libmemcached_functions_mysql.so”;

    此处省略多行……
    测试UDF是否安装成功:
    先添加Memcached,可以添加多台。

    mysql> select memc_servers_set(’127.0.0.1′);
    mysql> select memc_set(’libing’, ‘roast’);
    +—————————-+
    | memc_set(’libing’, ‘roast’) |
    +—————————-+
    | 0 |
    +—————————-+
    1 row in set (0.00 sec)
    mysql> select memc_get(’libing’);
    +———————————+
    | memc_get(’libing’) |
    +———————————+
    | roast |
    +———————————+
    1 row in set (0.00 sec)

    测试将MySQL数据映射到Memcached中,如果对MySQL的触发程序不熟悉可以参考MySQL手册第21章。

    mysql> create table memcached (`key` int, `value` varchar(100));
    Query OK, 0 rows affected (0.02 sec)

    mysql> create trigger mysqlmmc before insert on memcached for each row
    > set @tmp = memc_set(NEW.key, NEW.value);
    Query OK, 0 rows affected (0.00 sec)

    mysql> insert into memcached values(9,’roast’);
    Query OK, 1 row affected (0.01 sec)

    mysql> select memc_servers_set(’127.0.0.1′);
    +——————————-+
    | memc_servers_set(’127.0.0.1′) |
    +——————————-+
    | 58360307675824128 |
    +——————————-+
    1 row in set (0.00 sec)

    mysql> select memc_get(’9′);
    +—————+
    | memc_get(’9′) |
    +—————+
    | roast |
    +—————+
    1 row in set (0.00 sec)

    [root@nd-zf-mx ~]# telnet 127.0.0.1 11211
    Trying 127.0.0.1…
    Connected to nd (127.0.0.1).
    Escape character is ‘^]’.
    get 9
    VALUE 9 0 5
    roast
    END

    这里只实现了当数据库的新记录产生时候,同步更新Memcached的触发程序。
    当数据库有更新和删除操作时都需要同步更新Memcached从而达到映射的关系。

  • 相关阅读:
    Robot FrameWork基础学习(二)
    如何做好接口测试?
    性能测试之Jmeter学习(十)
    性能测试之Jmeter学习(九)
    python基础数据类型的相关知识点
    python基础知识 -- set集合
    python基础小知识,is和==的区别,编码和解码
    python中的数据类型之元组和字典
    python中 列表常用的操作
    python中的字符串(str)操作
  • 原文地址:https://www.cnblogs.com/cy163/p/1544143.html
Copyright © 2020-2023  润新知