• 将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从而达到映射的关系。

  • 相关阅读:
    React 获取服务器API接口数据:axios、fetchJsonp
    nvm管理node版本
    windows自定义命令的创建
    目标平台、活动平台 配置,出现未能加载文件或程序集“xxx”或它的某一个依赖项报错
    Quartz.net使用总结
    vs 2010 中类文文件模板的修改
    js获取url参数的两种方法
    遍历文件夹
    简单多条件动态查询的实现
    ajax请求跨域问题
  • 原文地址:https://www.cnblogs.com/cy163/p/1544143.html
Copyright © 2020-2023  润新知