Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8254721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:17:25+00:00 2026-06-08T01:17:25+00:00

Consider the following code: function getUser($uid) { global $_memcache; $u = $_memcache->get(user-.$uid); if( !$u)

  • 0

Consider the following code:

function getUser($uid) {
    global $_memcache;
    $u = $_memcache->get("user-".$uid);
    if( !$u) {
        $u = ... // get user from database
        $_memcache->set("user-".$uid,$u);
    }
    return $u;
}

Now, this function will probably be called a LOT. For instance, on the forum it may be called 30+ times in a single pageload. Is it worth doing the following, or is Memcache already optimal enough?

function getUser2($uid) {
    static $localcache = [];
    if( isset($localcache[$uid])) return $localcache[$uid];
    return $localcache[$uid] = getUser($uid);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:17:27+00:00Added an answer on June 8, 2026 at 1:17 am

    Well ultimately memcached is in memory storage, just as storing the values in PHP array would be. If the memcached instance is on the same server that you are running the code on, then I would definitely say just use memcached instead of both memcached and the PHP array, as you would end up just using ~ twice as much memory on the machine by storing the same data in memory twice.

    If memcached is on seperate infrastructure (which is probably likely), then I guess I would consider what the size of the data stored for the user is going to be. If it is large and you may store a large number of users in the PHP array, it means you could potentially be sucking up more memory on your web server than you find desirable, in which case accessing the data from memcached repeatedly, while being slightly slower for overall access time, would probably provide significant application performance under server load.

    Personally, I would probably tend to lean towards relying on memcached for each request, as it is just more scalable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following base code: (function($) { $.fn.myPlugin = function(settings) { return this.each(function() {
Let's consider the following scenario: a function which can generate code colors from white
Consider the following code: function authenticate(){ $this->session->set_userdata('logged_in', true); } How do I return this
Consider the following code? I was wondering, if I change it from (Function body
Consider the following code: var table = function () { return { _init: function
Consider the following code: var f = function() { return 10; } typeof f;
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following code: $(window).unload(function () { console.log('foo'); }); If I trigger the unload
Consider the following code: function Robot(weapon) { this.weapon = weapon; this.fire = function() {
Consider the following piece of code: function processParagraph(paragraph) { if (paragraph.charAt(0) === '%') {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.