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

  • SEARCH
  • Home
  • 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 6347583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:14:42+00:00 2026-05-24T21:14:42+00:00

I’m just trying to figure out how I can add memcache to a database

  • 0

I’m just trying to figure out how I can add memcache to a database class. I haven’t tested the code below (it’s even the first time I’m trying to add memcache to my code), but does it at least make sense? Or can someone think of a better way to do it?

Database Class (simplified)

database
{
    protected $result;

    protected $row;

    protected $cache_result;

    public function query($sql, $cache = false)
    {
        /* only use memcache for selected queries */

        if($cache)
        {
            $key = md5('query'.$sql);

            $this->result = $memcache->get($key);

            if($this->result == null)
            {
                $this->result = mysql_query($sql);

                $this->cache_result = mysql_fetch_object($sql);

                /* cache for 1 hour */

                $memcache->set($key, $this->cache_result, 0, 3600);
            }
        }
        else
        {
            $this->result = mysql_query($sql);
        }

        return $this->result;
    }

    public function fetch($sql)
    {
        $this->row = mysql_fetch_assoc($sql);

        return $this->row;
    }
}

PHP (simplified)

include_once $_SERVER['DOCUMENT_ROOT'].'/loader.php';

$sql = "SELECT * FROM `table` WHERE `this` = 'that'";

/* use memcache for the query below */

$a = $database->query($sql, true);

$b = $database->fetch($a);

PHP (loader)

$database = new database();

$memcache = new Memcache;

$memcache->connect('127.0.0.1', 11211);
  • 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-05-24T21:14:45+00:00Added an answer on May 24, 2026 at 9:14 pm

    I won’t vouch for the syntax as I don’t code PHP, however it looks like the right approach with the following observations.

    You shouldn’t really take the md5 of ‘query’.$sql. It would be better to do ‘query’.md5($sql) so that the prefix acts like a pseudo namespace in that you can guarantee that ‘ns1:’.md5($sql) won’t clash with ‘ns2:’.md5($sql). There are cleverer ways of using such pseudo namespaces that the following FAQ handles well.

    http://code.google.com/p/memcached/wiki/FAQ#How_do_I_access_memcached?

    The next thing I would say is I would be wary of having an explicit expiration time. It’s often better to let the cache handle which items to dump when it needs the space. If you have a busy site, you can find that explicit expirations will cause unnecessary cache dumps and performance issues that you are meant to be solving by adding the cache. There is a good starting point on some of the issues here;

    http://highscalability.com/strategy-break-memcache-dog-pile

    And given the title, I can’t resist linking to this article as well;

    http://blog.craigambrose.com/past/2007/11/13/caching_makes_your_brain_explode/

    I therefore tend to explicitly invalidate the appropriate cache keys when data changes, rather than using arbitrary time constraints. But either way, try to understand the consequences of whichever approach you are taking.

    BUT

    The first thing I would actually say is … have you tuned your database yet? In particular, are you using the query cache that can be enabled and configured? This is likely to have a far more dramatic impact on performance than adding memcached – until you are scaling well beyond the capacity of a single server of course.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.