简体   繁体   中英

php mysql and memcached

I'm a bit confused on memcached and PHP when used with SQL queries.

Practically every guide I've come across does something resembling this:

$querykey = "KEY" . md5($query);
$result = $mem->get($querykey);

What I don't understand, is why they all seem to store a hashed version of the actual query as the key?

Wouldn't it be better to give it a proper name, that way it's easier to reference anywhere in your script and invalidate it if needed?

Like this guide and this guide .

Yes, you can use anything you want as the key in memcached, within the key constraint supported by memcached ( a string 250 bytes or less ).

The key doesn't necessarily have anything to do with one specific SQL query. It might not be something that came from a database at all.

Some apps store whole fragments of HTML in memcached, like a <div> that is ready to use as a drop-down list in a web page for example. The content of which might include the result of several SQL queries, integrated into the HTML fragment.

The suggestion to use a hash of a specific SQL query is probably just an example shown in tutorials. Like naming your class "Foo" or "MyClass" — you wouldn't actually use those names in a real application.

PS: The two tutorials you link to are so similar, I suspect the one from November 2015 is borrowing some of its content from the earlier one from May 2014.

I think that is a good question. First Point generate an md5 hash is the easiest way to get a unique key of a string. So you don't need to generate a complicate key.

But in general you can use every key you want and build your own structure.

https://secure.php.net/manual/de/memcached.getallkeys.php

Here is a good example how to get all keys and delete them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM