Reputation: 48973
1)
I understand I can call this
$memcache_obj = memcache_connect('memcache_host', 11211);
in the header files of my site with no impact on pages that don't use memcache but what about this
$memcache->connect('127.0.0.1', 11211);
Should this be called on a page to page basis?
2)
what if the server does not have enough memory to write new caches and memcache tries to store a cache?
3)
I know keys can have up to a 30 day lifespan,
Is there a way to flush all keys out of memory, especially useful for testing phase?
4)
Also this code, the first variable is connecting, so for example if I have 5 sections on a page that add/update/delete from memcache, do I need to run this connection everytime, or is it possible to connect 1 time and do everything per page load?
memcache_set($memcache_obj, 'var_key', 'some variable', 0, 30)
5) Is there a function or anything to show like how much memory is available and being used on a php page?
Upvotes: 3
Views: 2427
Reputation: 722
Jason answered your questions very well but I thought that I would add some notes:
2) Note that if you try to store more than 1MB (default) into a key the memcache extension will return a FALSE value. It will also return FALSE if it can't write a key for any reason.
3) Keys can have a >30 day lifespan (TTL). Just add the TTL to the current time and use that as the TTL. Using your example call, it might be something like this (coded for clarity):
$ttl = 60*60*24*60; // 60 days
$newTTL = time()+$ttl;
memcache_set($memcache_obj, 'cache_key', 'some data', 0, $newTTL)
5) If you are talking about PHP memory then memory_get_usage()
will get you what you want. Memcache memory is a little harder to come by but using the getStats()
call will start you in the right direction.
Upvotes: 3
Reputation: 2049
yes, no network calls are made until an attempt to fetch, delete, etc..., so it does not hurt to allocate the object in case it is needed. (EDIT: I was thinking of the "memcached" extension here, turns out the "memcache" extension does in fact open a connection to the memcache server, though the hit is negligable at most)
memcache will drop least used items to attempt to make space for the new object
$memcache_obj->flush();
connect only needs to happen once per script run, easiest to place the connect at the top of your page or in a class constructer
$memcache_obj->getStats() http://www.php.net/manual/en/function.memcache-getstats.php
Upvotes: 8
Reputation: 48387
Upvotes: 1