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 6918847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:54:44+00:00 2026-05-27T09:54:44+00:00

I am beginning to add a memcached layer to my application. so far so

  • 0

I am beginning to add a memcached layer to my application. so far so good.

I realised quite quickly however that I will be needing to invalidate/delete large batches of keys in one go when someone uploads a file to the site in order to keep the data relevant.

I have done some reading and the most common way of getting around this problem seems to be setting a version number on each key, then rather than deleting the keys when a user uploads (because there could be so many permutations) you incrememnt the version number, inducing a cache miss next time the data is accessed.

I have no idea where to begin in order to get this coded and i’m not quite sure I totally get my head around it anyway.

My code currently looks like this:-

$memcache = new Memcache;


$memcache->connect('localhost', 11211) or die ("Could not connect");


$key = md5("songsearch_$textSearch.$rangeSearch");

The two variables in the key var above are retrieved from the get request which in turn retrieves a large amount of JSON. (Think a product catalogue).

These variables also determine the query itself which is dynamically put together dependant upon these variables. Ultimately all of this gives me a key that is unique to every individual query, even though from the same script I could have hundreds of keys being generated.

I hope the above is clear, if not please ask me to clarify any points in order to better help you answer my question.

Obviously later, to set the cache the results I am using this:-

$memcache->set($key, $output, MEMCACHE_COMPRESSED, time() + 24*60*60*365);

just before I encode the JSON.

So my question really is… How can I add some form of incremental versioning to this so that if a user uploads a file I can invalidate the whole lot of keys that have been generated by this script?

Huge thanks to anyone who gets me at least on the right track.

  • 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-27T09:54:44+00:00Added an answer on May 27, 2026 at 9:54 am

    You are obviously on the right track. The only thing you’re missing: Store a version number in the memcache, which you retrieve before you build your key.

    $memcache = new Memcache;
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    
    // fetch the current version number
    $version = $memcache->get('version_number');
    
    // and add the version to the key
    $key = md5("songsearch_$textSearch.$rangeSearch") . 'v' . $version;
    

    So now, whenever somebody uploads new content, you simply increment the version number:

    $memcache->increment('version_number');
    

    This automatically leads to all existing queries to run into invalid entries.

    To simplify the access, I recommend you wrap this in a new Memcache handler class (untested):

    class VersionedMemcache extends Memcache {
    
        const VERSION_KEY = 'version_number';
    
        private $version = 1;
    
        public function __construct() {
            $version = parent :: get(self :: VERSION_KEY);
            if ($version === false) {
                $version = 1;
                $this->set(self :: VERSION_KEY, $version);
            }
            $this->version = $version;
        }
    
        public function get($key, &$flags = null) {
            $key = $key . '_v' . $this->version;
            return parent :: get($key, $flags);
        }
    
        public function incVersion() {
            $this->version = $this->increment(self :: VERSION_KEY);
            return $this->version;
        }
    
    }
    

    So now, you would simply amend your script to:

    $memcache = new VersionedMemcache();
    
    $memcache->connect('localhost', 11211) or die ("Could not connect");
    
    $key = md5("songsearch_$textSearch.$rangeSearch");
    
    // this will now result in a fetch on 'ab23...232afe_v1' instead of 'ab23...232afe'
    $result = $memcache->get($key);
    
    // when an upload happens, simply
    $memcache->incVersion();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can add an element to a form like this: $form->addElement($element); However, that will
I'm trying to write a simple procedure that will add to a List every
Is it possible to add come comment at the beginning of files during checkout
What is the best way to add text to the beginning of a file
I want to be able to add a little note, at the beginning of
I'm really beginning to understand what people mean when they say that C++'s error
I have a MySQL table and want to add 'x' to the beginning of
We are in the beginning coding phase for project that we are using JPA
I'm trying to add at beginning and , at end of each non-empty line
How can I add an element to the beginning of array without changing array

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.