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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:12:34+00:00 2026-06-13T05:12:34+00:00

I’m trying to cache some results retrieved from database using Yii framework 1.1.12. Here

  • 0

I’m trying to cache some results retrieved from database using Yii framework 1.1.12. Here is what I am doing in short:

public static function getCategories()
{
    if (self::$_categories !== null)
        return self::$_categories;

    print "Getting categories...";    
    self::$_categories = Yii::app()->cache->get("categoriesList");
    if (self::$_categories === false)
    {
        $sql = "SELECT id, parent_id, name FROM {{category}} WHERE id > 0 AND is_deleted = 0";
        $categoriesList = Yii::app()->db->createCommand($sql)->queryAll();

        // Doing some work with $categoriesList and obtaining self::$_categories as the result
        // ...

        $dependency = new CDbCacheDependency("SELECT MAX(update_time) FROM {{category}}");
        Yii::app()->cache->set("categoriesList", self::$_categories, 3600, $dependency);
    }

    return self::$_categories;
}

Using the profiling tool I can see that everything works. At the first time both queries are executed (each query once):

SELECT MAX(update_time) FROM arrenda_category
SELECT id, parent_id, name FROM arrenda_category WHERE id > 0 AND is_deleted = 0

On further requests only first one is executed.

The problem is when I increase max value of update_time in arrenda_category table (even not using my own edit script – directly from MySQL command line) and refresh the page a count of SELECT MAX(update_time) FROM arrenda_category queries becomes equal to 2. Further refreshes give only one execution again. The interesting thing is if I clear the cache I have one execution of SELECT MAX(...) ... query too.

So I don’t understand why a query of cache dependency class is executed twice on condition’s change. Is there something wrong with my code or maybe anything else?

P.S. I’m sure that SELECT MAX(update_time) FROM arrenda_category can be executed only in function described above. I also see that the line print "Getting categories..." is reached once per page request.

  • 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-13T05:12:36+00:00Added an answer on June 13, 2026 at 5:12 am

    Yes. It is expected.

    EXPLANATION

    Suppose the data is already in the cache. And when you call the function getCategories() , the line Yii::app()->cache->get("categoriesList") will execute the dependancy query to check whether the data is changed. Since it is not changed the query is executed one time only.

    Now you changed the update_time value externally ( or using some another code in your app ), and you call the getCategories() again,

    1. The line Yii::app()->cache->get("categoriesList") executes the dependancy query to check whether the data in the cache is valid. It finds that data is invalid and returns false
    2. Then the query SELECT id, parent_id, name FROM {{category}} WHERE id > 0 AND is_deleted = 0 is executed to fetch the updated data from the database
    3. The line Yii::app()->cache->set("categoriesList", self::$_categories, 3600, $dependency); AGAIN executes the dependancy query SELECT MAX(update_time) FROM {{category}} to get the latest MAX(update_time) whose result is stored along with the data. Thats why the query is executed twice.

    So the point is that every time you set() a value to cache, the dependancy value must be stored along with it since it is needed for the subsequent get() queries for checking whether the dependency is changed.

    PS:
    If you want more clarification check the source code of the set() function of your cache application component ,it calls the evaluateDependency() function of the CDbCacheDependancy class which inturn calls the generateDependentData() which causes the execution of the dependancy query

    • 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
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a view passing on information from a database: def serve_article(request, id): served_article
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.