1

Say if you want to allow a user to be able to delete some data, but typically it is static and good for caching. Is there a way to remove that value from the key so you can keep everything else cached and get rid of what the user wanted deleted? The key would be an array of many arrays. The arrays hold the info for the item like id, user name, title, etc. and would want to remove one of those arrays if the user wants to delete.

Example code:

Storing stuff:

    $cachedData = $this->memcache->get($this->key);
    if($cachedData === false){
        $cachedData = array();
        $sql = "SELECT id, name FROM table LIMIT 100";
        $res = mysql_query($sql);
        while($rec = mysql_fetch_assoc($res)){
            $cachedData[] = $rec;
        }
        // cache for 10 minutes
        $this->memcache->set($this->key, $cachedData, 0, 600);
    } 

It's basically right from google code example.

4
  • Do you mean "remove one key-value pair, but keep others", or "remove something from a 'value' that contains multiple things"? Commented Mar 14, 2012 at 3:41
  • Sorry, I'm new. I believe I mean one key-value pair. So a simple example: SELECT id, name FROM table LIMIT 100 so you have those 100 put in the key and want to delete one of those. Commented Mar 14, 2012 at 3:44
  • That one row is not a key-value pair. If you have 100 rows stored in the value for a key, that's one key-value pair for all 100. The key is a single key, and the value is a set of 100 rows. Commented Mar 14, 2012 at 3:45
  • @Amber ok. Misunderstanding the nomencalture. Commented Mar 14, 2012 at 3:56

2 Answers 2

1

The only way to do this is to read the key's value into a variable, modify the contents of that variable, and then write the entire key back to memcached.

memcached operates completely at the top-level key:value layer; it doesn't care nor understand what you put in it as values. As far as it's concerned, that "array of arrays" you're putting in as a value is just another string.

Sign up to request clarification or add additional context in comments.

4 Comments

That was my thought process. I don't know if I should open another question but I'm not sure how to do that. When I store the into the key it ends up like array(2) { [0]=> {array(2) { 'id' => "1", 'name' => "bob"}}
Edit your question to show the code that you're using to store the values in the first place.
The answer of putting it in a variable and modifying it is what I see that I should do, unless that is bad and I should change the code for storing in memcached? Should I open another question in how to modify it becaus eI do not know.
Nope, putting it in a variable and modifying it is indeed what you should do.
0

What you seem to be describing is exactly what memcached does. So I'm thinking that either I misunderstood the question or that you didn't ask what you wanted to ask.

To delete a key-value pair, use Memcache::delete. Doing so will leave all other key-value pairs intact.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.