Cache methode help

Discussion in 'PHP' started by mhrlive, Apr 11, 2013.

  1. #1
    Hello,
    Im beginner in PHP dev. I want to reduce a DB select request on my application, by implementing a temp variable for every user, every x min that variable must be updated from the DB using a function getonline, note that every user have its own unique usercode and query result, so every user run the same function with same usercode during the next x min will get the result from the temp variable without accessing the DB.
    something like:
    If ($temp[$user_code] == null) {
    $temp[$user_code] = $this->getOnlineNow($user_code);
    return $temp[$user_code];
    }
    but that of course will never work.

    please dont respond with "go learn how memcached work" or "use read this", please help I want a simple solution Im a beginner !
    thanks :)
     
    Last edited: Apr 11, 2013
    mhrlive, Apr 11, 2013 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    $_SESSION? for example

    $_SESSION['online'] = array('time' => timestamp,
    'users' => 121);

    check if time > time + ?? secs and then refresh or don't do anything?

    But if you want multiple users to make use of this? create a cache function that writes the value to a file if the file doesn't exists or when the time has expired.

    If you like i can add a snippet from my very easy (and understandable) class.
     
    EricBruggema, Apr 11, 2013 IP
  3. mhrlive

    mhrlive Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #3
    $_SESSION['online'] = array('time' => timestamp,
    'users' => 121);

    check if time > time + ?? secs and then refresh or don't do anything?

    But if you want multiple users to make use of this? create a cache function that writes the value to a file if the file doesn't exists or when the time has expired.

    If you like i can add a snippet from my very easy (and understandable) class.[/quote][/quote]
    Hi Eric, thanks for reply, and thats my first idea and its not going to work with my case, because of multiple users with different IDs using the same function, waiting for your class :)
    Also I just installed memcached as last resort, and it seems good and easy after I watch this easy tutorial :


    Any one can help and write the code for multiple users ?
    $Im too tired now to try it, going to sleep :)
     
    mhrlive, Apr 11, 2013 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    EricBruggema, Apr 11, 2013 IP
  5. ChiragKalani

    ChiragKalani Active Member

    Messages:
    41
    Likes Received:
    2
    Best Answers:
    3
    Trophy Points:
    73
    #5
    Have you tried to use Cookie instead?
     
    ChiragKalani, Apr 12, 2013 IP
  6. mhrlive

    mhrlive Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #6
    Hello again, it return a simple int number
     
    mhrlive, Apr 12, 2013 IP
  7. mhrlive

    mhrlive Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #7
    Got it to work using memcached :)
    something like :
    $test = $memcache->get($code);
    if ($test == null)
    {
    $tempp = $this->getOnlineNow($code);
    $memcache->set($user_code, $tempp, false, 60); //###time in second
    }
    $onlinenow = $memcache->get($code);
    return $onlinenow;
    Code (markup):
    Wish that help future reader ;)
     
    mhrlive, Apr 12, 2013 IP
  8. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #8
    Good start, but there is no need to make a cache call if you already have the data:

    
    $results = $memcache->get($code);
    if ($results == null) {
        $results = $this->getOnlineNow($code);
        $memcache->set($code, $results, false, 60); //###time in second
    }
    return $results;
    
    Code (markup):
     
    ThePHPMaster, Apr 12, 2013 IP
  9. mhrlive

    mhrlive Active Member

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #9
    Yeah thanks for that ;)
     
    mhrlive, Apr 13, 2013 IP