tags:

views:

460

answers:

3

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?

+6  A: 
  1. 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)

  2. memcache will drop least used items to attempt to make space for the new object

  3. $memcache_obj->flush();

  4. 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

  5. $memcache_obj->getStats() http://www.php.net/manual/en/function.memcache-getstats.php

Jason
awsome thanks, you are the memcache guy I have seen many of your post on the issue. Also i added 1 more question above if you care to look
jasondavis
+1  A: 
  1. http://uk3.php.net/manual/en/function.memcache-connect.php suggests that memcache_connect() is the same as instantiating a Memcache and calling connect() on it
  2. http://code.google.com/p/memcached/wiki/FAQ#What_is_memcached%27s_cache? says "If the server is out of memory, expired slabs are replaced first, then the oldest unused slabs go next."
  3. Restart the memcached instance, or call flush, which will invalidate all cache items on the server
Rob
+3  A: 

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.

conceptDawg