PHP memcached更新结果
I'm about to implement a memcached class which can be extended by our database class. However i have looked at many different ways of doing this.
My First question was, what is the point in the Memcached::set()
as it seems to replace the value of the key. Does this not defeat the object of caching your results?
My Second question was, technically speaking what is the fastest/best way to update the value of a key without checking the results every time the query is executed. If this happens, the results been retreived would have to be checked constantly so there would be no point in caching as the results would constantly be connecting to the mysql database.
Lastly, What is the best way of creating a key? most people recommend using MD5 however using MD5 on a PDO query would be the same regardless.
I.E.
$key = MD5("SELECT * FROM test WHERE category=?");
the category could produce many different results however the key would be constantly replaced. Is there a best practice for this?
我即将实现一个memcached类,可以通过我们的数据库类进行扩展。 但是我已经看了很多不同的方法。 p>
我的第一个问题是, 我的第二个问题是,从技术上讲,更新密钥值的最快/最佳方法是什么,而不是每次都检查结果 查询已执行。 如果发生这种情况,则必须不断检查结果已被检查,因此缓存没有意义,因为结果将不断连接到mysql数据库。 p>
最后,什么是 创建密钥的最佳方法? 大多数人建议使用MD5,但无论如何在PDO查询中使用MD5都是相同的。 p>
IE p>
该类别可能产生许多不同的结果,但密钥会不断被替换。 这是最好的做法吗? p>
div> Memcached :: set() code>中的重点是什么 替换密钥的值。 这不会破坏缓存结果的对象吗? p>
$ key = MD5(“ SELECT * FROM test WHERE category =?“);
code> pre>
You set a cache entry when you had to read the database, so that next time, you don't have to read the database first. You'd check the cache, and if it was not there, or otherwise out of date, then you fall back to the database read, and reset the key.
As for a key name, it depends very much on the expected values of the category. If if was a simple integer, or string, I'd use a key like test.category:99
or test.category:car
. If it was likely to be more, it may be useful to encode it, so there were no spaces in it (say, urlencode).
Finally, if it were any more complex than that - test:category:{MD5(category)}
.
Since the key is only a reference to the data and you'll never be using it in any kind of SQL query, putting the value in there is not generally going to be a security issue.
Since you control when the cache is set, if the underlying database entry is changed, it's simple to also update the cache with the new data at the same time - you just have to use the same key.