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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:59:31+00:00 2026-05-17T01:59:31+00:00

I am thinking of integrating some caching logic in my app. Mainly, it will

  • 0

I am thinking of integrating some caching logic in my app.

Mainly, it will cache objects and objects lists to APC. I will
implement a way for them to be automatically invalidated, once the object
is updated/removed.

However, what about atomicity? How can I make sure that operations are atomic?

  • 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-17T01:59:31+00:00Added an answer on May 17, 2026 at 1:59 am

    Well, if you’re talking about a single operation, it will be atomic (based upon how APC works). Either it will all be written, or none of it will…

    If you’re talking about multiple operations (As in updating every place an object is referenced), then there’s nothing built in to prevent a race condition. Instead, what you’ll need to do is implement some sort of locking to prevent other processes from trying to update that data.

    One way you can do this is to “lock” each cached item by writing a special lock “cached” item to a known id before starting the operation (think row level locking in databases).

    So, assuming an array of affected cache ids:

    function getLocks($cacheIds) {
        $locked = array();
        foreach ($cacheIds as $cacheId) {
            if (apc_exists($cacheId . '_locked')) {
                //Another process has this id locked, clear all locks and return
                foreach ($locked as $id) apc_delete($id . '_locked');
                return false;
            } else {
                //Use a short TTL to prevent issues if this process dies
                apc_store($cacheId . '_locked', 60);
                $locked[] = $cacheId;
            }
        }
        return true;
    }
    
    function releaseLocks($cacheIds) {
        foreach ($cacheIds as $cacheId) {
            apc_delete($cacheId . '_locked');
        }
    }
    

    So, then you can simply call:

    if (getLocks($cacheIds)) {
        //Do your operation here
        releaseLocks($cacheIds);
    }
    

    Now, beware that this doesn’t prevent the tiny probability of two processes checking for the same key at the same time (and hence both returning false for apc_exists, yet overwriting each other). If this is a big problem, then you should read up on double-checked locking

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

Sidebar

Related Questions

I was thinking about integrating some instant messaging function into an existing ASP.NET web
I'm working on a small application and thinking about integrating BLAST or other local
Im thinking this is correct, but probably WAY off. I have a string formatted
Just thinking about the best way to build an Order form that would (from
Im thinking about learn to develop app for iOS. I had a lot of
I'm thinking of integrating a chat inside an already written and working php application.
I am thinking about some issues which may match the term software integration best.
I have some code to add attachments to an email. I'm adding them via
Here is what I am thinking: When a user browses some other page, or
I'm learning Mockito. Before starting to use mock objects I had some Unit tests

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.