- Drupal Clear Cache_form Table
- Drupal Cache Control
- Drupal Cache Form Extension
- Drupal Cache Form Definition
- Drupal Safe Cache Form Clear
- 4.6.x includes/bootstrap.inc cache_set()
- 4.7.x includes/bootstrap.inc cache_set()
- 5.x includes/cache.inc cache_set()
- 6.x includes/cache.inc cache_set()
- 6.x includes/cache-install.inc cache_set()
Stores data in the persistent cache.
Cache form Drupal change expire time. Ask Question Asked 6 years, 10 months ago. Active 3 years, 7 months ago. Viewed 1k times 2. Drupalflushallcaches is the function called from systemclearcachesubmit , the submission form handler called when you click on the 'Clear all caches' button, in the performance settings page. During cron tasks, systemcron clears the cache using the following code. If you'd like to clear an entire cache bin for database caches, regardless of the 'expire' value for a cached object, you need to do something like: cacheclearall('.', 'cachemymodulebin', TRUE); Providing an '.' for the first parameter tells the database cache interface's clear method to truncate the particular cache. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Stores data in the persistent cache. The persistent cache is split up into several cache bins. In the default cache implementation, each cache bin corresponds to a database table by the same name.
The persistent cache is split up into several cache bins. In the defaultcache implementation, each cache bin corresponds to a database table by thesame name. Other implementations might want to store several bins in datastructures that get flushed together. While it is not a problem for mostcache bins if the entries in them are flushed before their expire time, somemight break functionality or are extremely expensive to recalculate. Theother bins are expired automatically by core. Contributed modules can addadditional bins and get them expired automatically by implementinghook_flush_caches().
The reasons for having several bins are as follows:
- Smaller bins mean smaller database tables and allow for faster selects andinserts.
- We try to put fast changing cache items and rather static ones intodifferent bins. The effect is that only the fast changing bins will need alot of writes to disk. The more static bins will also be better cacheablewith MySQL's query cache.
Parameters
$cid:The cache ID of the data to store.
$data:The data to store in the cache. Complex data types will be automaticallyserialized before insertion. Strings will be stored as plain text and arenot serialized. Some storage engines only allow objects up to a maximum of1MB in size to be stored by default. When caching large arrays or similar,take care to ensure $data does not exceed this size.
$bin:(optional) The cache bin to store the data in. Valid core values are:
- cache: (default) Generic cache storage bin (used for theme registry,locale date, list of simpletest tests, etc.).
- cache_block: Stores the content of various blocks.
- cache_bootstrap: Stores the class registry, the system list of modules,the list of which modules implement which hooks, and the Drupal variablelist.
- cache_field: Stores the field data belonging to a given object.
- cache_filter: Stores filtered pieces of content.
- cache_form: Stores multistep forms. Flushing this bin means that someforms displayed to users lose their state and the data already submittedto them. This bin should not be flushed before its expired time.
- cache_menu: Stores the structure of visible navigation menus per page.
- cache_page: Stores generated pages for anonymous users. It is flushedvery often, whenever a page changes, at least for every node and commentsubmission. This is the only bin affected by the page cache setting onthe administrator panel.
- cache_path: Stores the system paths that have an alias.
$expire:(optional) Controls the maximum lifetime of this cache entry. Note thatcaches might be subject to clearing at any time, so this setting does notguarantee a minimum lifetime. With this in mind, the cache should not beused for data that must be kept during a cache clear, like sessions.
Use one of the following values:
- CACHE_PERMANENT: Indicates that the item should never be removed unlessexplicitly told to using cache_clear_all() with a cache ID.
- CACHE_TEMPORARY: Indicates that the item should be removed at the nextgeneral cache wipe.
- A Unix timestamp: Indicates that the item should be kept at least untilthe given time, after which it behaves like CACHE_TEMPORARY.
See also
_update_cache_set()
cache_get()
- archiver_get_infoin includes/common.inc
- Retrieves a list of all available archivers.
- book_menu_subtree_datain modules/book/book.module
- Gets the data representing a subtree of the book hierarchy.
- CacheClearCase::testClearArrayin modules/simpletest/tests/cache.test
- Test clearing using an array.
- CacheClearCase::testClearCidin modules/simpletest/tests/cache.test
- Test clearing using a cid.
- CacheClearCase::testClearWildcardin modules/simpletest/tests/cache.test
- Test clearing using wildcard.
File
- includes/cache.inc, line 141
- Functions and interfaces for cache handling.
Code
Comments
To clear a specific cache item...
...pass $cid and $bin to the misleadingly named cache_clear_all()
Update Link
Example of using cache in function
Wrong expire time?
Shouldn't the expire time be added to the current time?
Like this:
Logic error
cache_set() is being called on every invokation. It should only be called when the data is rebuilt.
Use REQUEST_TIME rather than
Use REQUEST_TIME rather than time().... but yeah you'd need to treat it as a timestamp so 360 would be some time in 1970.
Edit: this was supposed to be a reply to chrisroane's comment but I guess I clicked the wrong reply link.
Good catch!
Couldn't Get This to Work
The only way I could get the caching to work as expected in a custom function was to also use &drupal_static(__FUNCTION__) ....Below is a simplified version of what I got working:
General Cache Wipe Details
The $expire
option CACHE_TEMPORARY says
Indicates that the item should be removed at the next general cache wipe.
How is the next general cache wipe triggered? Is it triggered by cron jobs? Is it triggered every hour or every day?
cron
@technicalknockout Yes, it happens on cron run by default, so how often depends on what you have cron set to.
General Cache wipe
Is there any way that i can wipe cache after some minutes?
Try the Elysia cron module
Try the Elysia cron module for more fine grained control of the cron service. If that is not enough, implement cache_clear_all() or take a look at the code of drupal_flush_all_caches() for more ideas.
Not 100% sure but i think Rules could also do this.
Expire cache with Unix timestamp using strtotime()
Example with strtotime(), maybe for a more clean and easy way for getting a cache to expire in 6 hours:
This way I can have a select form element with values like '+1 hour', '+6 hour', '+1 day', etc...
Cheers!
Very nice and clean example.
Poor Documentation
This is very lame but the $expire parameter, when set in a form of a Unix timestamp, is ignored if the parameter cache_lifetime set in admin/config/development/performance is not set to X minutes.
This will cause cache_get to return expired entries!
Sadly, the description for the cache_lifetime parameter is incorrect because it mentions 'cached pages' so one would assume that it's applicable to whole pages and not specific cache entries.
This is not documented and should not even happen otherwise it destroys the purpose of setting an expire parameter.
Expire does nothing
I can confirm there is an issue with the $expire parameter. I spent last night testing this issue out with a simple retrieval of data and storing it to the cache and setting the $expire to time() + 300 (5 minutes). I then printed out the time, the time the cache was set and expiration. The time the cache was set and expiration worked fine, but I noticed that even after 30 minutes this cache would still persist.
A simple way to go around this was to check if time() was bigger than the $cache->expire and then clearing out the cid with cache_clear_all(). An example code is below.
if ($cached = cache_get('db
if ($cached = cache_get('db_index', 'cache')) {
$tables = $cached->data;
}
else {
$tables = drupal_get_schema();
// Setting the data to drupal cache.
cache_set('db_index', $tables, 'cache', CACHE_TEMPORARY);
}
Drupal
NETivism
User can see the lastest post
客戶說他的網頁不會更新
Display blank page randomly
隨機出現白畫面
My css effect has gone
CSS的效果不見了
Form can't submit.
本來正常的表單,卻再也送不出去了
Everyone saw broken page but not on my computer.
別人看網頁壞掉,但我看起來是好的
Why my counter not increase?
為什麼我的文章點閱沒增加?
Only a word comes to mind...
Peopo.org
- over 100,000 nodes (not many)
- over 800,000 node-category relation
- over 300 query in page (when no cache)
- over 140 enabled drupal modules
- heavy query (join large table)
31 ms for render page
to anonymous user
0ms for 304 not modified
376 ms for render page
to logged in user
Cache control in http header
- Only for anonymous visitor
- Tell browser take control of cache
- Tell browser this page will expire after 21600s
- Drupal already done in default:
read: drupal_serve_page_from_cache
Etag / Expires / Last Modified
in HTML header
- Only for anonymous visitor
- Etag:
- Add unique id to delivered page
- Last-Modified:
- Used to detect if page modified since last visit.
- Expires:
- Setting by 'max-age' of heder cache-control
Static file cache in browser
- Do not write CSS / Javascript in page
- drupal_add_css in ALL PAGE
- durpal_add_js should consider too
Nginx
- Combination of proxy / static http file server
- We won't hit Dynamic if we don't need
- Extreme low in memory comsumption
- Build-in cache mechanism
- Build-in flood control mechanism
- Asynchronous event-driven handle request
- Eat ton of request in 1 process
- More effective in multi-processor env
microcache in Nginx
- Flood Protection / DOS protection
- Short lifetime ( 1s - 5s )
- Can used for logged user (use carefully)
- Never hit PHP when continue hitting
Boost.module of Drupal
- Only for anonymous visitor
- Static HTML page generation when first hit on PHP
- Use rewrite rule (in Nginx or Apache) to redirect to HTML
- Expired by
- Cron
- Crawler ( drupal 6 only)
- Node update ( drupal 6 only )
Serve static html from nginx
10x faster than Apache prefork
nginx: 100,000 requests in 60s
apache: 10,000 requests in 60s
Drupal Clear Cache_form Table
What Drupal does from fresh hit?
Warm up PHP in every request
- Load system wide variables
- Load menu to check user have permission to access page
- Load localized data to prepare translation strings
- Load modules to prepare work
- Render forms when needed
- Gethering content in Blocks of page
- Deliver page rendered by template / theme system
- Finally we got an page! So slow...
Run SQL Query in every needs
OP Code Cache for PHP
Drupal Cache Stack
almost cache everything
- Page ( only for anonymous user )
- Variables
- Session cache ( 3 party module )
- Localized string
- Module info/registry
- Template engine registry
- Filtered content
- Forms
- Aggregated content (Block/Views/Panels)
- Aggregated resource (CSS/JS/Image)
Views Cache (in period or ...)
Panels Cache (per page or ...)
Block Cache (set by module code)
Recommended Modules for cache
- Memcache API and Integration or Memcache Storage
- save cache into memcached (instead hitting database)
- Views Content Cache
- cache views based on content save
more effective because not time period - Panel Page Cache or Panel Hash Cache
- cache panels based on url / user / content type
- setting cache clear rules in simple clicks (with rules)
- Claw specific pages you need.
Cache Strategy
- cache in Time Period
- cache per User
- cache per Page (URL)
- cache per User per Page
- cache per Role per Page
- others
- cache per content type
- cache per node
- cache per role
- ...
Cache Clear Strategy
- Expired after time period
- Expired when new version exists
- clear node cache when node update
- clear views / panels cache when node update
- clear user profile page when user update
- clear term page when term update
- Expired in specific URL parameter
Drupal Cache Control
Drupal is a Query Monster
Drupal Cache Form Extension
- Join , Join, Join, Join .......
- Eat 300 query a page easily
- Save config / variables in DB
- Save session in DB
- Save cache in DB
- Save localized string in DB
- Save search index in DB
- Un-optimized query from views
- Everything combined with node
Hot news page
Query Cache Performance
Before query cache: 2301.12ms
After query cache: 2.49ms
'Watch' your views
Calculate Hit Rate
Server / Service Combination
- PHP-APC
- Almost 100% hit rate
- Watch out - some version ignore .module file
- Do not use in multiple virtualhost environment
- MySQL Query Cache
- 32MB memory get huge performance enhance
- Remember calculate hit rate
- Memcached
- client-server architect
- use even behind balancer
- Session should save here, but D7 still problem
Anonymous User
- microcache of nginx
- 1-3s cache time is enough
- ajax needed for Drupal node page counting
- Best practice at perusio/ drupal-with-nginx
- Page Cache to memcached
- drupal core mechnism but save cache to memory
- Trouble-less, very stable, but still hitting PHP
- recommend set cache max lifetime
- Cache Warmer of drupal
- simple drush command to crawl specific page in cron
Drupal Cache Form Definition
Logged in User
- Panels + Panel page cache
- Cache whole panel based on url
- Hack module to support per role per page
- Fix some personal string in ajax
- do not cache form
- Use views cache carefully
- can't run without this
- headache on Contextual / Exposed filter cache
- finally, we can add 'update - then - clear cache' rule
- got to have debug skill at fetch 'cache id'
Basic Question
- Do the cache enabled?
- What I'm seeing, cached or not?
- When the cache expired or refresh?
- Is that correct when different user see this page?
(user name or profile misplace) - Is that correct when different role see this page?
(permission reason)
- Do the form worked correctly?
- you can't cache form (security reason)
Tools and Hint
- Page cache can verify by HTTP header
- PHP-APC havewatch script
- Memcached have script, too
- Use devel module to see page generate time
- Profiling: XHProf
- on large site, never 'Clear All Cache'
(this will save you life) - no cached page when anonymous have session
(added at drupal 7)