yiwai 发表于 2018-11-6 06:48:48

初学Redis(2)——用Redis作为Mysql数据库的缓存

  用Redis作Mysql数据库缓存,必须解决2个问题。首先,应该确定用何种数据结构存储来自Mysql的数据;在确定数据结构之后,还要考虑用什么标识作为该数据结构的键。
  直观上看,Mysql中的数据都是按表存储的;更微观地看,这些表都是按行存储的。每执行一次select查询,Mysql都会返回一个结果集,这个结果集由若干行组成。所以,一个自然而然的想法就是在Redis中找到一种对应于Mysql行的数据结构。Redis中提供了五种基本数据结构,即字符串(string)、列表(list)、哈希(hash)、集合(set)和有序集合(sorted set)。经过调研,发现适合存储行的数据结构有两种,即string和hash。
  要把Mysql的行数据存入string,首先需要对行数据进行格式化。事实上,结果集的每一行都可以看做若干由字段名和其对应值组成的键值对集合。这种键值对结构很容易让我们想起Json格式。因此,这里选用Json格式作为结果集每一行的格式化模板。根据这一想法,我们可以实现将结果集格式化为若干Json对象,并将Json对象转化为字符串存入Redis的代码:
   view plaincopyhttps://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

[*]  // 该函数把结果集中的每一行转换为一个Json格式的字符串并存入Redis的STRING结构中,
[*]  // STRING键应该包含结果集标识符和STRING编号,形式如“cache.string:123456:1”
[*]  string Cache2String(sql::Connection *mysql_connection,
[*]  redisContext *redis_connection,
[*]  sql::ResultSet *resultset,
[*]  const string &resultset_id, int ttl) {
[*]  if (resultset->rowsCount() == 0) {
[*]  throw runtime_error("FAILURE - no rows");
[*]  }
[*]  // STRING键的前缀,包含了结果集的标识符
[*]  string prefix("cache.string:" + resultset_id + ":");
[*]  unsigned int num_row = 1;// STRING编号,附加于STRING键的末尾,从1开始
[*]  sql::ResultSetMetaData *meta = resultset->getMetaData();
[*]  unsigned int num_col = meta->getColumnCount();
[*]  // 将结果集中所有行对应的所有STRING键存入该SET,SET键包含了结果集的标识符
[*]  string redis_row_set_key("resultset.string:" + resultset_id);
[*]  redisReply *reply;
[*]  string ttlstr;
[*]  stringstream ttlstream;
[*]  ttlstream beforeFirst();
[*]  // 将结果集中的每一行转为Json格式的字符串,将这些Json字符串存入STRING,
[*]  // 每个STRING对应结果集中的一行
[*]  while (resultset->next()) {
[*]  string redis_row_key;// STRING键名,由前缀和STRING编号组成
[*]  stringstream keystream;
[*]  keystream rowsCount() == 0) {
[*]  throw runtime_error("FAILURE - no rows");
[*]  }
[*]  // HASH键的前缀,包含了结果集的标识符
[*]  string prefix("cache.hash:" + resultset_id + ":");
[*]  unsigned int num_row = 1;// HASH编号,附加于HASH键的末尾,从1开始
[*]  sql::ResultSetMetaData *meta = resultset->getMetaData();
[*]  unsigned int num_col = meta->getColumnCount();
[*]  // 将结果集中所有行对应的所有HASH键存入该SET,SET键包含了结果集的标识符
[*]  string redis_row_set_key("resultset.hash:" + resultset_id);
[*]  redisReply *reply;
[*]  string ttlstr;
[*]  stringstream ttlstream;
[*]  ttlstream beforeFirst();
[*]  while (resultset->next()) {
[*]  string redis_row_key;// HASH键名,由前缀和HASH编号组成
[*]  stringstream keystream;
[*]  keystream
页: [1]
查看完整版本: 初学Redis(2)——用Redis作为Mysql数据库的缓存