Advertisement
Guest User

SE Platform Cache Structure

a guest
Jan 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. //wrapper class that is used in the cache
  2. global class MenuWrapper{        
  3.     public List<FieloEE__Section__c> sections {get; set;}
  4.     public Map<Id,List<FieloEE__Section__c>> componentsSection {get; set;}
  5.     public Map<Id,FieloEE__Component__c> componentsMap {get; set;}
  6.     public Set<Id> componentCacheIds;
  7. }
  8.  
  9. //Action method
  10. public PageReference init(){    
  11.     loadSections();        
  12.     return null;
  13. }
  14.  
  15. private void loadSections(){        
  16.     //verifies if the cache functionality is enabled
  17.     cacheEnabled = FieloEE__PublicSettings__c.getOrgDefaults().CacheEnabled__c;
  18.     if(cacheEnabled != null && cacheEnabled){
  19.         loadSectionsWithCache();            
  20.     }else{
  21.         loadSectionsWithoutCache();
  22.     }
  23.  
  24. }
  25.  
  26. private void loadSectionsWithoutCache(){
  27.    
  28.     //instantiate wrapper class
  29.     menuWrapper = new MenuWrapper();
  30.    
  31.     //queries sections
  32.     menuWrapper.sections = [SELECT Id, Name, F_PRM_Asynchronous__c, FieloEE__Type__c, FieloEE__Order__c, FieloEE__Menu__c, FieloEE__CSSClasses__c FROM FieloEE__Section__c WHERE FieloEE__Menu__c =: menu2.Id ORDER BY FieloEE__Order__c ASC NULLS FIRST,CreatedDate ASC];
  33.    
  34.     //query subsections with components related lists
  35.     List<FieloEE__Section__c> sections = menuWrapper.sections;
  36.     for(FieloEE__Section__c subSection : Database.query('SELECT Id, FieloEE__Type__c, FieloEE__Order__c, Name, FieloEE__Menu__c, FieloEE__Parent__c, FieloEE__CSSClasses__c, (SELECT ' + fields + ' FROM FieloEE__Components__r ORDER BY FieloEE__Order__c asc) FROM FieloEE__Section__c WHERE FieloEE__Parent__c IN : sections ORDER BY FieloEE__Order__c ASC')){
  37.         if(!menuWrapper.componentsSection.containsKey(subSection.FieloEE__Parent__c)){
  38.             menuWrapper.componentsSection.put(subSection.FieloEE__Parent__c, new List<FieloEE__Section__c>{subSection});
  39.         }else{
  40.             menuWrapper.componentsSection.get(subSection.FieloEE__Parent__c).add(subSection);
  41.         }
  42.         //load componentsMap
  43.         FieloEE.PageController.componentsMap.putAll(subSection.FieloEE__Components__r);
  44.     }
  45.  
  46.     //the componentCacheIds is null due cache is inactive
  47. }
  48.  
  49. private void loadSectionsWithCache(){        
  50.    
  51.     //Get platform cache partition
  52.     orgPart = Cache.Org.getPartition('local.FieloMenus');
  53.    
  54.     //generates menu key
  55.     keyString = String.valueOf(menu2.Id).subString(0,15);
  56.    
  57.     //get value from the cache for specified key
  58.     menuWrapper = (MenuWrapper)orgPart.get(keyString);
  59.    
  60.     //checks if the key exists in the cache, if no then loads the wrapper
  61.     if(menuWrapper == null){
  62.         loadMenuWrapper(keyString);
  63.     }
  64.    
  65. }
  66.  
  67. private void loadMenuWrapper(String keyString){
  68.    
  69.     //instantiates the wrapper
  70.     menuWrapper = new MenuWrapper();
  71.    
  72.     //query sections
  73.     menuWrapper.sections = [SELECT Id, Name, F_PRM_Asynchronous__c, FieloEE__Type__c, FieloEE__Order__c, FieloEE__Menu__c, FieloEE__CSSClasses__c FROM FieloEE__Section__c WHERE FieloEE__Menu__c =: menu2.Id];
  74.  
  75.     //query subsections
  76.     for(FieloEE__Section__c subSection : Database.query('SELECT Id, FieloEE__Type__c, FieloEE__Order__c, Name, FieloEE__Menu__c, FieloEE__Parent__c, FieloEE__CSSClasses__c, (SELECT ' + fields + ' FROM FieloEE__Components__r ORDER BY FieloEE__Order__c asc) FROM FieloEE__Section__c WHERE FieloEE__Parent__c IN : sectionsList AND FieloEE__Parent__r.F_PRM_Asynchronous__c = false ORDER BY FieloEE__Order__c ASC')){
  77.        
  78.         //load subsections into the wrapper
  79.         if(!menuWrapper.componentsSection.containsKey(subSection.FieloEE__Parent__c)){
  80.             menuWrapper.componentsSection.put(subSection.FieloEE__Parent__c, new List<FieloEE__Section__c>{subSection});
  81.         }else{
  82.             menuWrapper.componentsSection.get(subSection.FieloEE__Parent__c).add(subSection);
  83.         }
  84.  
  85.         //load components into the wrapper
  86.         menuWrapper.componentsMap.putAll(subSection.FieloEE__Components__r);
  87.        
  88.         //loads in the wrapper set the component Ids that are set to be cached (content cache)
  89.         for(FieloEE__Component__c c : subSection.FieloEE__Components__r){
  90.             if(c.Cached__c){
  91.                 menuWrapper.componentCacheIds.add(c.Id);
  92.             }
  93.         }
  94.     }
  95.  
  96.     //save the wrapper into the cache
  97.     orgPart.put(keyString,menuWrapper);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement