SlideShare a Scribd company logo
1 of 26
MUC
Moodle Universal Cache
Tim Hunt
Introduction
What is a cache
A place to keep data that would take time to re-compute
A cache stores values identified by a key
E.g. list of activities in a course, stored by course id
Caches may get purged at any time
Cannot store real data there, just derived values for
performance
Why have a cache system?
Developers want to do stuff with data, not re-implement
caching
Want to be able to use different back-end stores
Disc, Memcache, REDIS, …
cachestore_ is a plugin type
Admins should control which data goes in which store
Cache performance
Typical call to cache::get For a typical page
• 0.5 ms data fetched from Memcache 150
• 0.05 ms data in static acceleration 700
For comparison: typical DB query
• 3 ms 40
Using caches
Typical use
$cache = cache::make('core', 'string');
$strings = $cache->get($component);
if ($strings === false) {
$strings = load_strings($component);
$cache->set($component, $strings);
}
Defining your cache
In db/caches.php in your plugin. E.g. from lib/db/caches.php
$definitions = array(
// Used to store processed lang files.
// Keys used are revision, lang and component of the string file.
// Static acceleration size is based on student access of the site.
'string' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true,
'simpledata' => true,
'staticacceleration' => true, // Metadata helps cache
'staticaccelerationsize' => 30, // system handle the data
'canuselocalstore' => true, // efficiently.
),
);
$string['cachedef_string'] = 'Language string cache';
Types of cache
Application – Data that applies throughout Moodle
E.g. list of activities in each course / language strings
Session – Data that is relevant while a user is logged in
E.g. list of all course categories this user can access
Request – Data within the handling of one request
E.g. list of temporary tables
Clearing the cache
$cache = cache::make('core', 'questiondata');
$cache->delete($questionid);
cache::make('core', 'questiondata')->purge();
Recommended: just clear out what has changed
Dangerous
Also not recommended: time-to-live in cache definition
Bulk actions
$cache->get_many(['key1', 'key2']);
// ['key1' => 'value1', 'key2' => 'value2']
$cache->set_many(
['key1' => 'value1', 'key2' => 'value2']);
$cache->delete_many(['key1', 'key2']);
Advanced use: data source
Data source: why?
Avoids the normal
“is this in the cache? Yes: good; No, re-compute”
Tell the cache where to get the data if it is not yet stored
Data source: definition
$definitions = array(
// Cache for question definitions. This is used by the question
// bank class. Users probably do not need to know about this cache.
// They will just call question_bank::load_question.
'questiondata' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true, // The id of the question is used.
'requiredataguarantee' => false,
'datasource' => 'question_finder',
'datasourcefile' => 'question/engine/bank.php',
),
)
class question_finder implements cache_data_source {
public static function get_instance_for_cache(cache_definition $definition) {
return self::get_instance(); // Singleton pattern.
}
public function load_for_cache($questionid) {
global $DB;
$questiondata = $DB->get_record_sql('SELECT q.*, qc.contextid
FROM {question} q
JOIN {question_categories} qc ON q.category = qc.id
WHERE q.id = :id', array('id' => $questionid), MUST_EXIST);
get_question_options($questiondata);
return $questiondata;
}
}
Data source: use
$cache = cache::make('core', 'questiondata');
$cache->get($questionid);
Data source: implementation
public function get($key, $strictness = IGNORE_MISSING) {
// ...
$result = $this->store->get($key);
// ...
if ($result === false) {
if ($this->loader !== false) {
$result = $this->loader->get($key);
} else {
if ($this->datasource !== false) {
$result = $this->datasource->load_for_cache($key);
}
}
}
// ...
}
Cache administration
Cache administration
E.g. https://learn2acct.open.ac.uk/cache/admin.php
Cache administration: continued
Cache administration:
continued
Cache administration: continued
Issues
Memcache purging
MUC lets you store many different caches in one backend
cache::purge(); should just clear one of those caches
With Memcache, cache::purge() wipes all caches
This is one of the reasons the VLE crashed in October
Moodle 3.2 now has REDIS cache store in core
We should consider switching
Complex keys
MUC tries to support keys that are not just ints or strings
However this hurts performance – best not to use it
This probably applies to other advanced MUC features
Caching is about performance
Simple is good
Automated tests
MUC caches are automatically cleared between each unit
test or Behat test
Useful links
https://docs.moodle.org/dev/Cache_API
https://docs.moodle.org/dev/Cache_API_-_Quick_reference
cache/README.md in the code
https://docs.moodle.org/dev/The_Moodle_Universal_Cache_(MUC)

More Related Content

What's hot

What's hot (20)

Introduction to burp suite
Introduction to burp suiteIntroduction to burp suite
Introduction to burp suite
 
Spring Security
Spring SecuritySpring Security
Spring Security
 
Hack like a pro with burp suite - nullhyd
Hack like a pro with burp suite - nullhydHack like a pro with burp suite - nullhyd
Hack like a pro with burp suite - nullhyd
 
Spring Security
Spring SecuritySpring Security
Spring Security
 
Spring framework
Spring frameworkSpring framework
Spring framework
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
Burp suite
Burp suiteBurp suite
Burp suite
 
Hunting for security bugs in AEM webapps
Hunting for security bugs in AEM webappsHunting for security bugs in AEM webapps
Hunting for security bugs in AEM webapps
 
Robot framework en Browser Library
Robot framework en Browser LibraryRobot framework en Browser Library
Robot framework en Browser Library
 
Burp Suite v1.1 Introduction
Burp Suite v1.1 IntroductionBurp Suite v1.1 Introduction
Burp Suite v1.1 Introduction
 
Dive in burpsuite
Dive in burpsuiteDive in burpsuite
Dive in burpsuite
 
Automação de Teste para iOS e Android com Appium
Automação de Teste para iOS e Android com AppiumAutomação de Teste para iOS e Android com Appium
Automação de Teste para iOS e Android com Appium
 
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
Not Just ORM: Powerful Hibernate ORM Features and CapabilitiesNot Just ORM: Powerful Hibernate ORM Features and Capabilities
Not Just ORM: Powerful Hibernate ORM Features and Capabilities
 
Introduction to Software Test Automation
Introduction to Software Test AutomationIntroduction to Software Test Automation
Introduction to Software Test Automation
 
Web Hacking With Burp Suite 101
Web Hacking With Burp Suite 101Web Hacking With Burp Suite 101
Web Hacking With Burp Suite 101
 
크로스(멀티)브라우저 테스트수행가이드
크로스(멀티)브라우저 테스트수행가이드크로스(멀티)브라우저 테스트수행가이드
크로스(멀티)브라우저 테스트수행가이드
 
Burp suite
Burp suiteBurp suite
Burp suite
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Güvenlik Testlerinde Açık Kaynak İstihbaratı Kullanımı
Güvenlik Testlerinde Açık Kaynak İstihbaratı KullanımıGüvenlik Testlerinde Açık Kaynak İstihbaratı Kullanımı
Güvenlik Testlerinde Açık Kaynak İstihbaratı Kullanımı
 
Hacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sitesHacking Adobe Experience Manager sites
Hacking Adobe Experience Manager sites
 

Similar to MUC - Moodle Universal Cache

Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
Fabien Potencier
 
CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
jonswar
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
Fabien Potencier
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
Fabien Potencier
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
Fabien Potencier
 
Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010
Fabien Potencier
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Fabien Potencier
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
chuvainc
 

Similar to MUC - Moodle Universal Cache (20)

Built-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIsBuilt-in query caching for all PHP MySQL extensions/APIs
Built-in query caching for all PHP MySQL extensions/APIs
 
Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3Dependency Injection with PHP and PHP 5.3
Dependency Injection with PHP and PHP 5.3
 
Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3Dependency Injection with PHP 5.3
Dependency Injection with PHP 5.3
 
CHI-YAPC-2009
CHI-YAPC-2009CHI-YAPC-2009
CHI-YAPC-2009
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
Drupal 7 Queues
Drupal 7 QueuesDrupal 7 Queues
Drupal 7 Queues
 
Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4Dependency injection in PHP 5.3/5.4
Dependency injection in PHP 5.3/5.4
 
Dependency Injection IPC 201
Dependency Injection IPC 201Dependency Injection IPC 201
Dependency Injection IPC 201
 
Dependency injection - phpday 2010
Dependency injection - phpday 2010Dependency injection - phpday 2010
Dependency injection - phpday 2010
 
Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010Dependency Injection - ConFoo 2010
Dependency Injection - ConFoo 2010
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
Decouple Your Code For Reusability (International PHP Conference / IPC 2008)
 
About Data::ObjectDriver
About Data::ObjectDriverAbout Data::ObjectDriver
About Data::ObjectDriver
 
Lazy evaluation drupal camp moscow 2014
Lazy evaluation drupal camp moscow 2014Lazy evaluation drupal camp moscow 2014
Lazy evaluation drupal camp moscow 2014
 
Caching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment CachingCaching and Scaling WordPress using Fragment Caching
Caching and Scaling WordPress using Fragment Caching
 
Intro to advanced caching in WordPress
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPress
 
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
 
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
 
Speed Things Up with Transients
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with Transients
 
mysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handlermysqlnd query cache plugin: user-defined storage handler
mysqlnd query cache plugin: user-defined storage handler
 

More from Tim Hunt

More from Tim Hunt (15)

Writing better Behat tests
Writing better Behat testsWriting better Behat tests
Writing better Behat tests
 
What’s next for the Quiz and Question bank and for Moodle community collabora...
What’s next for the Quiz and Question bank and for Moodle community collabora...What’s next for the Quiz and Question bank and for Moodle community collabora...
What’s next for the Quiz and Question bank and for Moodle community collabora...
 
Question bank improvements in Moodle 4.0 : A successful community collaboration
Question bank improvements in Moodle 4.0 : A successful community collaborationQuestion bank improvements in Moodle 4.0 : A successful community collaboration
Question bank improvements in Moodle 4.0 : A successful community collaboration
 
Looking after the Open University's Moodle
Looking after the Open University's MoodleLooking after the Open University's Moodle
Looking after the Open University's Moodle
 
Embedding questions anywhere in Moodle
Embedding questions anywhere in MoodleEmbedding questions anywhere in Moodle
Embedding questions anywhere in Moodle
 
Hosting STACK at scale
Hosting STACK at scaleHosting STACK at scale
Hosting STACK at scale
 
Moodle questions without the quiz
Moodle questions without the quizMoodle questions without the quiz
Moodle questions without the quiz
 
2017 UK/IE MoodleMoot: What makes a good moodle quiz? Lessons from the Open U...
2017 UK/IE MoodleMoot: What makes a good moodle quiz? Lessons from the Open U...2017 UK/IE MoodleMoot: What makes a good moodle quiz? Lessons from the Open U...
2017 UK/IE MoodleMoot: What makes a good moodle quiz? Lessons from the Open U...
 
I wish I could believe you: the frustrating unreliability of some assessment ...
I wish I could believe you: the frustrating unreliability of some assessment ...I wish I could believe you: the frustrating unreliability of some assessment ...
I wish I could believe you: the frustrating unreliability of some assessment ...
 
Hosting Moodle at the OU
Hosting Moodle at the OUHosting Moodle at the OU
Hosting Moodle at the OU
 
The Moodle quiz at the Open University
The Moodle quiz at the Open UniversityThe Moodle quiz at the Open University
The Moodle quiz at the Open University
 
The Moodle Quiz at the Open University: how we use it & how that helps students
The Moodle Quiz at the Open University: how we use it & how that helps studentsThe Moodle Quiz at the Open University: how we use it & how that helps students
The Moodle Quiz at the Open University: how we use it & how that helps students
 
2012 Computer-Assisted Assessment
2012 Computer-Assisted Assessment2012 Computer-Assisted Assessment
2012 Computer-Assisted Assessment
 
Moodle’s building blocks for eAssessment tools
Moodle’s building blocks for eAssessment toolsMoodle’s building blocks for eAssessment tools
Moodle’s building blocks for eAssessment tools
 
A basic introduction to the Moodle architecture
A basic introduction to the Moodle architectureA basic introduction to the Moodle architecture
A basic introduction to the Moodle architecture
 

Recently uploaded

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 

Recently uploaded (20)

Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
Abortion Pill Prices Mthatha (@](+27832195400*)[ 🏥 Women's Abortion Clinic In...
 
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
Workshop -  Architecting Innovative Graph Applications- GraphSummit MilanWorkshop -  Architecting Innovative Graph Applications- GraphSummit Milan
Workshop - Architecting Innovative Graph Applications- GraphSummit Milan
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024Food Delivery Business App Development Guide 2024
Food Delivery Business App Development Guide 2024
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Turfloop ](+27832195400*)[ 🏥 Women's Abortion Clinic in ...
 
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdfThe Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
The Evolution of Web App Testing_ An Ultimate Guide to Future Trends.pdf
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 

MUC - Moodle Universal Cache

  • 3. What is a cache A place to keep data that would take time to re-compute A cache stores values identified by a key E.g. list of activities in a course, stored by course id Caches may get purged at any time Cannot store real data there, just derived values for performance
  • 4. Why have a cache system? Developers want to do stuff with data, not re-implement caching Want to be able to use different back-end stores Disc, Memcache, REDIS, … cachestore_ is a plugin type Admins should control which data goes in which store
  • 5. Cache performance Typical call to cache::get For a typical page • 0.5 ms data fetched from Memcache 150 • 0.05 ms data in static acceleration 700 For comparison: typical DB query • 3 ms 40
  • 7. Typical use $cache = cache::make('core', 'string'); $strings = $cache->get($component); if ($strings === false) { $strings = load_strings($component); $cache->set($component, $strings); }
  • 8. Defining your cache In db/caches.php in your plugin. E.g. from lib/db/caches.php $definitions = array( // Used to store processed lang files. // Keys used are revision, lang and component of the string file. // Static acceleration size is based on student access of the site. 'string' => array( 'mode' => cache_store::MODE_APPLICATION, 'simplekeys' => true, 'simpledata' => true, 'staticacceleration' => true, // Metadata helps cache 'staticaccelerationsize' => 30, // system handle the data 'canuselocalstore' => true, // efficiently. ), ); $string['cachedef_string'] = 'Language string cache';
  • 9. Types of cache Application – Data that applies throughout Moodle E.g. list of activities in each course / language strings Session – Data that is relevant while a user is logged in E.g. list of all course categories this user can access Request – Data within the handling of one request E.g. list of temporary tables
  • 10. Clearing the cache $cache = cache::make('core', 'questiondata'); $cache->delete($questionid); cache::make('core', 'questiondata')->purge(); Recommended: just clear out what has changed Dangerous Also not recommended: time-to-live in cache definition
  • 11. Bulk actions $cache->get_many(['key1', 'key2']); // ['key1' => 'value1', 'key2' => 'value2'] $cache->set_many( ['key1' => 'value1', 'key2' => 'value2']); $cache->delete_many(['key1', 'key2']);
  • 13. Data source: why? Avoids the normal “is this in the cache? Yes: good; No, re-compute” Tell the cache where to get the data if it is not yet stored
  • 14. Data source: definition $definitions = array( // Cache for question definitions. This is used by the question // bank class. Users probably do not need to know about this cache. // They will just call question_bank::load_question. 'questiondata' => array( 'mode' => cache_store::MODE_APPLICATION, 'simplekeys' => true, // The id of the question is used. 'requiredataguarantee' => false, 'datasource' => 'question_finder', 'datasourcefile' => 'question/engine/bank.php', ), )
  • 15. class question_finder implements cache_data_source { public static function get_instance_for_cache(cache_definition $definition) { return self::get_instance(); // Singleton pattern. } public function load_for_cache($questionid) { global $DB; $questiondata = $DB->get_record_sql('SELECT q.*, qc.contextid FROM {question} q JOIN {question_categories} qc ON q.category = qc.id WHERE q.id = :id', array('id' => $questionid), MUST_EXIST); get_question_options($questiondata); return $questiondata; } } Data source: use $cache = cache::make('core', 'questiondata'); $cache->get($questionid);
  • 16. Data source: implementation public function get($key, $strictness = IGNORE_MISSING) { // ... $result = $this->store->get($key); // ... if ($result === false) { if ($this->loader !== false) { $result = $this->loader->get($key); } else { if ($this->datasource !== false) { $result = $this->datasource->load_for_cache($key); } } } // ... }
  • 23. Memcache purging MUC lets you store many different caches in one backend cache::purge(); should just clear one of those caches With Memcache, cache::purge() wipes all caches This is one of the reasons the VLE crashed in October Moodle 3.2 now has REDIS cache store in core We should consider switching
  • 24. Complex keys MUC tries to support keys that are not just ints or strings However this hurts performance – best not to use it This probably applies to other advanced MUC features Caching is about performance Simple is good
  • 25. Automated tests MUC caches are automatically cleared between each unit test or Behat test