[opencms-dev] CmsJspNavBuilder->getSiteNavigation

T.Kluge at t-systems.com T.Kluge at t-systems.com
Mon May 26 14:28:38 CEST 2014


We found a really nice solution which I would like to share:

The following code stores a menu object in the CmsMemoryObjectCache.
Every real used user group combination is stored as separate menu object.
This decreased our login time from 15 seconds to 1 second.
The cache is cleared by restarting tomcat or calling clear kernel cache in the OpenCMS UI.

I tried to delete the TNavExpand items from cache by the PUBLISH event, but I failed, because I did not found any way to enumerate cache items and to destroy them depending on their class.


Best wishes,
Thomas



	/*
		TNavExpand is a custum menu class which stores menuitems
		it is built using the CmsJspNavBuilder function
	*/
	TNavExpand tnav;
	
	CmsUser currentUser = cms.getRequestContext().getCurrentUser();
	String sHash = getGroupMembershipHash(currentUser.getName());				

	// try to get the menu from cash
	tnav = (TNavExpand)CmsMemoryObjectCache.getInstance().getCachedObject(TNavExpand.class, sHash);		

	// memu object does not exist
	if(tnav == null)
	{
		// crate a new menu object and save it in the cache
		tnav = new TNavExpand(pageContext, request, response, iRootLevel, iMaxLevel);
		CmsMemoryObjectCache.getInstance().putCachedObject(TNavExpand.class, sHash, tnav);		
	}
	
	
	
	
	/* returns MD5 hash of all groups this user belongs to */
	public String getGroupMembershipHash(String strUser) {

		try {
			String sGroups = "Groups:";
			List<CmsGroup> vGroups = this.cms.getGroupsOfUser(strUser, true);
			for (Iterator<CmsGroup> en = vGroups.iterator(); en.hasNext();) {
				CmsGroup gr = en.next();
				sGroups+=gr.getName()+",";
			}
			
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(sGroups.getBytes());
			byte[] digest = md.digest();
			StringBuffer sb = new StringBuffer();
			for (byte b : digest) {
				sb.append(String.format("%02x", b & 0xff));
			}
			return sb.toString(); 
		} catch (Exception ex) {
			java.util.logging.Logger.getLogger(
					java.util.logging.Logger.GLOBAL_LOGGER_NAME).warning(
					"Exception in getGroupMembershipHash:" + ex.toString());
		}
		return null;
	}




More information about the opencms-dev mailing list