#1
  1. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123

    Logic for using memcache


    I'm looking at the memcache extension to aid with database select results and dynamic image caching.

    However, I haven't been able to test yet (i broke test my server, so also fixing that at the moment, then will install memcache).

    Now maybe it should be obvious (but it's not explicit) that if a cached value has expired it won't be returned (or the return value is false),

    PHP Code:
    //

    //bootstrap
    $memcache_obj = new Memcache;
    $memcache_obj->connect('memcache_host'11211);


    //logic

    $var $memcache_obj->get('some_key');

    if(
    $var) {
         
    //use cached value
         
    $result $var;
    } else {
         
    //make fresh data
         
    $result someData();
         
    $memcache_obj->set('some_key',$result,0,3600);
    }

    //confine with $result; 
    lets say that the expire time has been and gone - what does get() return? the expired value? (pretty useless) false (a little better)?

    Given that a return value of false could mean failure, not found or empty (which may be a valid result)....is there any way to test for what it was?

    Is there a way to see if there's a stale value?

    If the value has expired should I use set() or replace() to set it with the fresh value?

    Does anyone using memcache periodically flush everything (eg at the end of the day)?
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    When it expires, it's not there anymore, just like if you tried to fetch a random string.

    From the manual:
    Originally Posted by The Manual
    Returns: Returns the string associated with the key or an array of found key-value pairs when key is an array. Returns FALSE on failure, key is not found or key is an empty array.
    Don't store the literal value "false" if your logic requires a specific action only when the variable exists and is false. Store either the string, or the number zero.

    Replace() should only be used if the key already exists, though set() works on pre-existing keys anyway, may as well just use that all the time.


    Does anyone using memcache periodically flush everything (eg at the end of the day)?
    Our outsourced team does. I think it's dumb for what they're doing, but it may be valid for your instance.
    Last edited by ManiacDan; January 9th, 2013 at 09:54 AM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    So, what's gone wrong?

    PHP Code:
    //

    set_time_limit(60);

    $memcache = new Memcache;

    $memcache->connect('memcache_host'11211);

    $key 'foo';
    $val 'bar';

    $memcache->set($key$valnull, (time()+20));

    sleep(10);

    $var $memcache->get($key);

    var_dump($var);

    sleep(15);

    $var $memcache->get($key);

    var_dump($var); 
    outputs

    Code:
    boolean false
    boolean false
    whereas I was expecting the first to be string (len 3) 'bar'
    Last edited by Northie; January 9th, 2013 at 02:51 PM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    set() returns false on failure, make sure that's working.

    Is your memcache host really called memcache_host? Is it not localhost?

    the third argument to set() is technically an int. Try zero instead of null.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I have to laugh at myself sometimes at what I do and don't understand.

    It helps to know that 'memcache_host' is not just an arbitrary string but should actually point to a 'real' host.

    I did this and then thought that if it should point to a real host then there should be something at that host listening for requests.

    So I installed memcached and started the service (I actually thought that this had already been done when building the php memcache extension). All works well now!
    Last edited by Northie; January 9th, 2013 at 03:42 PM.
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,853
    Rep Power
    6351
    Nicely done.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo