I have a cache block in my template file like
{cache-block keys=$gallery.node_id subtree_expiry=$gallery.node_id expiry=0}
<div>
{def $sales_price =fetch('content', 'node', hash( 'node_id', 5564))}
{attribute_view_gui attribute=$sales_price.data_map.body}
</div>
{/cache-block}
Do the fetch query run every time? Or once the content is cached it will not run before the cache expires?
The content of a cache block is processed only when it's expired and needs to be regenerated.
So the answers are :
Do the fetch query run every time? => no, only when the block expires
Or once the content is cached it will not run before the cache expires? => yes
Related
I'm trying to reduce my symfony webpage loading time by using the cache tag in template and using doctrine cache in repository, like this :
In TWIG :
{% cache "navbar;v1;" ~ last_updated_at %}
{# loops, conditions and HTML ...#}
{% endcache %}
In REPOSITORY
...
return $this->createQueryBuilder('p')
->complexQuery...
->getQuery()
->setHint(Query::HINT_FORCE_PARTIAL_LOAD, true)
->useResultCache(true, 14400)
->getResult();
The first time I load the page I have a high load time and many SQL queries :
Then I refresh the page and it's much better :
I keep this tab open, I move to another task for let's say 15mn then I go back to that page and refresh it : I have something similar to the first screenshot :(
Cache seems to be purged or browser seems to ask a fresh version of the page ! Why the cache is acting like this ? Knowing data was not updated and cache should not be purged in that case !
Consider the following, using Apollo Client's Query Component:
<Query {...props} query={NOTEBOOK_QUERY} variables={{ id: props.notebookId }} >
{result => props.children(result)}
</Query>
the first time this runs i expect it to be a network call, and the second time I expect it to hit the cache.
Is there tooling that I can plug into to print out if it was a cache hit or miss, and what key was used?
Thanks!!
If you use the Apollo Dev Tools in Chrome, you can see the queries that have been cached in the Apollo Link State and it should show you whether it hit a result stored in the link state or went back to the database.
One of the main purposes of caching is to save resources and not do things like hit your database every request. In light of this, I'm confused by what all Codeigniter does in a controller when it encounters a cache() statement.
For example:
$this->output->cache(5);
$data=$this->main_model->get_data_from_database();
$this->load->view("main/index", $data);
I realize that the cached main/index html file will show for the next 5 minutes, but during these 5 minutes will the controller still execute the get_data_from_database() step? Or will it just skip it?
Note: the Codeigniter documentation says you can put the cache() statement anywhere in the controller function, which confuses me even more about whats getting executed.
I can answer my own question. NOTHING in the controller function other than the cached output gets executed during the time in which the cache is set.
To test this yourself, do a database INSERT or something that would be logged somehow (e.g. write to a blank file).
I added the following code below my cache() statement and it only inserted into the some_table table the first time I loaded the controller function and not the 2nd time (within the 5 minute span).
$this->db->insert('some_table', array('field_name' => 'value1') );
I think this can be verified enabling the Profiler in your controller and check if any query is done. Make sure this is enabled only for your IP if you're using it in Production environment.
$this->output->enable_profiler(TRUE);
-- EDIT 1 --
This will be visible only once. Soon after the cached page is stored, the profiles result won't be visible again (so you might wanna delete the file and refresh the page).
-- EDIT 2 --
You might also use:
log_message('info', 'message');
inside your model, then change in config.php, $config['log_threshold'] to 3 and check the log file.
-- EDIT 3 --
For sure the selection will be done unless you have enabled the database cache. In this case, in the cache folder you'll see the database selection cached.
I'm not sure if this can be done at all.
I'm trying to set Dalli to delete a memcache after 3 seconds (just to experiment)
dalli = Dalli::Client.new
dalli.add("test1","value", 3)
dalli.get("test1").should eql "value"
sleep(10)
dalli.get("test1").should eql nil
In the code, I have set TTL for 3 seconds, and then I expect that after 3 seconds the "test1" would be deleted but apparently not. So, the test fails in the second assertion. How can I ask Dalli to expire a key/value after a certain amount of time?
Thanks a lot.
You can only explicitly remove a key by calling delete or implicitly via TTL.
Does this happen with a different key besides 'test1'? Try changing your 'add' command to a 'set' command. The add command is conditional, it only sets the value if doesn't already exist. Is it possible you already set that key previously (although unlikely from the code you provided) without specifying a TTL? If you didn't specify a TTL the item is cached indefinitely.
Put the following in your session_store.rb initializer
Rails.application.config.session_store ActionDispatch::Session::CacheStore, :expire_after => 20.minutes
That will expire the cache after 20 minutes.
Using the devel module I can see a lot of calls to cache_get() and cache_set(). After how long does a cached value need to be refreshed? Does the cache get invalidated every few minutes?
The module that is using cache_set sets the expiration in the call. Some things have explicit durations, others have permanent or semi-permanent lifetimes, based on the situation.
Caches get explicitly cleared when you invoke the method through the admin interface (or drush), or otherwise through the use of drupal_flush_all_caches or cache_clear_all.
Lately, I have been using a hook_cron to clear certain cache tables each night.
EDIT to answer comment:
To see which cache, I usually put this in a separate script somewhere:
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
header("Content-Type: text/plain; encoding=utf-8");
$user = user_load(1);
print "Modules implementing hook_cron:\n" . implode("\n", module_implements('cron'));
To see expirations, examine the various cache tables in the database and look at the expire column. Modules can set expirations on each individual call to cache_set, so it can vary entry by entry.