代码之家  ›  专栏  ›  技术社区  ›  bear

使用APC在阵列中存储数据

  •  1
  • bear  · 技术社区  · 14 年前

    我目前正试图在我的Web应用程序中实现作为数据存储的APC缓存。

    目前,系统直接从MySQL数据库中检索数据,并且每个请求都需要一个数据库调用。

    我目前正试图通过用在每次请求时从缓存截获并提供服务的数据预填充缓存来更改这一点。

    现在的方法是:

    if(!empty($_GET['id'])){        
    
            $app = $db->real_escape_string($_GET['id']);
            $result = $db->query("SELECT * FROM pages_content WHERE id = $app");
            $rawdata = $result->fetch_assoc();
    

    }

    数据通过以下方式输出:

    $title = stripslashes($rawdata['title']);
    $meta['keywords'] = stripslashes($rawdata['htmlkeywords']);
    $meta['description'] = stripslashes($rawdata['htmldesc']);
    $subs = stripslashes($rawdata['subs']);
    $pagecontent = "<article>".stripslashes($rawdata['content'])."</article>";
    

    我需要预填充脚本对数据表中的每一行数据进行缓存。然后,服务脚本将能够在需要时从缓存中提取数据,使用诸如 apc_fetch('[columname][id]') .

    我怎么能设计出这个?

    我想我需要序列化数据?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Gabriel Sosa    14 年前

    <?php
    foreach ($row = $mylsql->get_row()) {
      $key = 'content_' . $row['id'];
      apc_store($key, $row);
    }
    

    lazy loading

    <?php
    $id = $_GET['id'];
    $key = 'content_' . $id; 
    $data = apc_fetch($key);
    if (!is_array($data)) {
      // call mysql here
      $data = $mylsql->get_row();
      apc_store($key, $data);
    
    }
    
    // your script here using $data
    

    cache invalidation

    推荐文章
    Matteo Codogno  ·  使用APC和MAMP
    10 年前