RDS可以和云数据库Memcache、云数据库Redis搭配使用,组成高吞吐、低延迟的存储解决方案。下面以云数据库Memcache与RDS的搭配使用为例介绍缓存数据持久化方案。

背景信息

与RDS相比,云数据库缓存产品有如下两个特性:

  • 响应速度快,云数据库Memcache和云数据库Redis请求的时延通常在几毫秒以内。

  • 缓存区能够支持比RDS更高的QPS(每秒处理请求数)。

环境要求

  • 本地环境或者ECS已安装bmemcached(支持SASL扩展)。

    bmemcached 下载地址:https://github.com/jaysonsantos/python-binary-memcached

    bmemcached 命令如下:

    pip install python-binary-memcached
  • 以Python为例,本地或者EC 环境需安装python、pip。

代码示例

云数据库Memcache与RDS搭配使用的代码示例如下。

#!/usr/bin/env python
import bmemcached
Memcache_client = bmemcached.Client((‘ip:port’), ‘user’, ‘passwd’)
#在云数据库Memcache中查找某个值
res = os.client.get(‘test’)
if res is not None:
    return res     #查询到值则返回
else:
    #未查询到值,则在RDS查询操作
    res = mysql_client.fetchone(sql)
    Memcache_client.put(‘test’, res) #向云数据库Memcache中写入缓存数据
    return res