使用APC将数据存储在数组中

发布于 2024-09-30 00:05:18 字数 881 浏览 6 评论 0原文

我目前正在尝试在我的 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]') 等内容从缓存中提取数据。

我怎么能设计出这个呢?

我想我需要序列化数据?

I'm currently attempting to implement APC caching as a datastore in my web application.

Currently, the system retrieves data directly from the MySQL database, and requires a database call per request.

I'm currently attempting to change this by prepopulating the cache with data which is intercepted and served from the cache at each request.

Here's the current method:

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();

}

Data is presented by output by:

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

What I need the prepopulating script to do is for each row of data in the data table, cache each row's data. The serving script would then be able to pluck the data from the cache as an when required, using something such as apc_fetch('[columname][id]').

How could I devise this?

I assume I'd need to serialize the data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天邊彩虹 2024-10-07 00:05:18

我真的不知道你缓存架构,所以回复不能准确。
首先:请记住,APC 使用服务器的共享内存,在这种情况下,如果您有多个服务器,它们都将至少获取一次数据库以获取数据。
如果您尝试按列存储,则需要确保创建某种锁定,否则您将遇到竞争条件,因为也许当您保存列时任何其他列都可能会更改。
我建议您完全保存该行,只需执行以下操作:

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

但另一方面,这意味着如果您有 1000 篇文章,您会将它们保存在缓存中,也许其中许多根本不会被读取。

在这种情况下,我建议您使用 延迟加载

<?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

这样您只缓存经常出现的内容打。

另一方面,请与您的缓存失效保持一致,以避免缓存中存在旧数据。

I really don't know you cache schema so the reply can't be exact.
First: remember that APC uses the server's shared memory, in which case if you have multiple server they all will fetch at least once the db to get the data.
If you try to store per column you need to be sure of create some sort of lock otherwise you will be having race conditions since maybe when you are saving a column any other could change.
What I recommend you is to save the row completely, just doing:

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

But in the other hand that means if you have 1000 articles you will save them in the cache and maybe many of them are not read at all.

Int that case I would recommend you use 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

this way you only cache the content that is often hit.

In the other hand please be consistent with your cache invalidation, to avoid having old data in the cache.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文