与APC缓存键组 [英] Key groups with APC cache

查看:134
本文介绍了与APC缓存键组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

APC可以让您存储钥匙内的数据,但你不能将这些密钥。

APC lets you store data inside keys, but you cannot group these keys.

所以,如果我想有一个名为文章组,该组中我有拿物品ID的形式,我不能做到这一点很容易键。

So if i want to have a group called "articles", and inside this group I would have keys that take the form of the article ID I can't do this easily.

articles -> 5   -> cached data
         -> 10  -> cached data
         -> 17  -> cached data

         ...

我可以preFIX关键的组的名称,如:

I could prefix the key with the "group" name like:

article_5   -> cached data
article_10  -> cached data
article_17  -> cached data

 ...

但是,这使得无法删除整个组,如果我想:(

But this it makes it impossible to delete the entire group if I want to :(

一个可行的解决方案。将存储多维数组(这是我现在在做什么),但我不认为这是一件好事,因为当我想访问/或删除缓存的数据,我需要得到整个集团第一。因此,如果组中它有一个数不胜数的文章,你可以像什么类型的数组我将迭代和搜索

A working solution would be to store multidimensional arrays (this is what I'm doing now), but I don't think it's good because when I want to access / or delete cached data, I need to get the entire group first. So if the group has one zillion articles in it you can image what kind of array I will be iterating and searching

你对我怎么能实现集团的事情更好的想法?

Do you have better ideas on how could I achieve the group thing?



修改发现了另一种解决办法,不知道这是否是要好得多,因为我不知道是怎么可靠呢。我加入所谓的特殊键 __路径这基本上就是一个包含pfixed关键路径,在缓存中的所有其他项目的全部$ P $多维数组。而当我请求或删除我用这个数组作为参考缓存中快速找出我需要删除键(或键组),所以我没有存储阵列和循环槽所有键...


edit: found another solution, not sure if it's much better because I don't know how reliable is yet. I'm adding a special key called __paths which is basically a multidimensional array containing the full prefixed key paths for all the other entries in the cache. And when I request or delete the cache I use this array as a reference to quickly find out the key (or group of keys) I need to remove, so I don't have to store arrays and iterate trough all keys...

推荐答案

基于你的观察,我看着底层C实现的 APC 的缓存模型( apc_cache.c ),看看我能找到。

Based upon your observations, I looked at the underlying C implementation of APC's caching model (apc_cache.c) to see what I could find.

该人士证实了你的意见,即没有分组结构中的后备数据存储的存在,使得对象的任何松散集合分组需要根据一些空间约束或修改缓存层本身工作要做。我所希望找到一些病毒通过链表的方式依靠关键链接,但不幸的是它似乎碰撞由碰撞槽代替的

The source corroborates your observations that no grouping structure exists in the backing data store, such that any loosely-grouped collection of objects will need to be done based on some namespace constraint or a modification to the cache layer itself. I'd hoped to find some backdoor relying on key chaining by way of a linked list, but unfortunately it seems collisions are reconciled by way of a direct reallocation of the colliding slot instead of chaining.

进一步混淆这个问题,APC似乎使用显式高速缓存模式用户条目,$ P $老化断pventing他们。因此,周华健Vikström提供的解决方案,它依靠的的LRU 模型的 memcached的的会,遗憾的是,无法正常工作。

Further confounding this problem, APC appears to use an explicit cache model for user entries, preventing them from aging off. So, the solution Emil Vikström provided that relies on the LRU model of memcached will, unfortunately, not work.

在不修改APC本身的源$ C ​​$ C,这里就是我会做:

Without modifying the source code of APC itself, here's what I would do:


  1. 定义一个命名空间约束条目符合。正如你原来上面定义,这将是类似文章_ prepended您的每一个条目。

  1. Define a namespace constraint that your entries conform to. As you've originally defined above, this would be something like article_ prepended to each of your entries.

定义的独立的这组元素的列表。实际上,这将是 5 10 17 你在上面会说明,但在这种情况下,你可以使用一些数值类型,使这个不是存储一大堆的字符串值更有效的方案。

Define a separate list of elements in this set. Effectively, this would be the 5, 10, and 17 scheme you'd described above, but in this case, you could use some numeric type to make this more efficient than storing a whole lot of string values.

定义一个的接口的来更新这套指针和后盾内存缓存调和他们,包括(至少)的方法插入删除明确。当明确被称为,走您的每一个指针,重建你的后备数据存储使用的密钥,每个从缓存中刷新。

Define an interface to updating this set of pointers and reconciling them with the backing memory cache, including (at minimum) the methods insert, delete, and clear. When clear is called, walk each of your pointers, reconstruct the key you used in the backing data store, and flush each from your cache.

我正在倡导这里是执行你寻求的的操作的良好定义的对象有效的。这与您的子缓存条目数线性扩展,但由于您使用的是数字式的每个元素,你需要超过100万个条目,或使你开始在制约体验真正的内存痛过,例如,几百兆

What I'm advocating for here is a well-defined object that performs the operations you seek efficiently. This scales linearly with the number of entries in your sub-cache, but because you're using a numeric type for each element, you'd need over 100 million entries or so before you started to experience real memory pain at a constraint of, for example, a few hundred megabytes.

塔马斯Imrei打我暗示替代战略我已经在记录的过程,但是这有一些重大我想缺陷展开讨论。

Tamas Imrei beat me to suggesting an alternate strategy I was already in the process of documenting, but this has some major flaws I'd like to discuss.

作为后盾C $ C $中所定义, APCIterator 是在整个数据集公共__construct(字符串$缓存[,混合$ =搜索一空的线性时间的操作...]]))。

As defined in the backing C code, APCIterator is a linear time operation over the full data set when performing searches (using its constructor, public __construct ( string $cache [, mixed $search = null ...]] )).

这是在背元素,你正在寻找重新present的总数据的一小部分,因为它会走的每一个元素在缓存中找到您需要的那些情况下断然不可取。引用 apc_cache.c

This is flatly undesirable in the case where the backing elements you're searching for represent a small percentage of your total data, because it would walk every single element in your cache to find the ones you desire. Citing apc_cache.c:

/* {{{ apc_cache_user_find */
apc_cache_entry_t* apc_cache_user_find(apc_cache_t* cache, char *strkey, \
  int keylen, time_t t TSRMLS_DC)
{
    slot_t** slot;
    ...
    slot = &cache->slots[h % cache->num_slots];
    while (*slot) {
        ...
        slot = &(*slot)->next;
    }
}

因此​​,我最强烈建议使用一个高效的,基于指针的虚拟分组解决您的问题,因为我上面勾勒出来。虽然,在你感觉严重的内存限制的情况下,迭代的方法可能是最正确的计算为代价来节省尽可能多的内存可能。

Therefore, I would most strongly recommend using an efficient, pointer-based virtual grouping solution to your problem as I've sketched out above. Although, in the case where you're severely memory-restricted, the iterator approach may be most correct to conserve as much memory as possible at the expense of computation.

祝您好运与您的应用程序。

Best of luck with your application.

这篇关于与APC缓存键组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆