What is the best way to cache OpenMRS Concept server side

In Bahmni, we are using OpenMRS concept resource to retrieve a concept. We have a use case to retrieve the same concept multiple times which is the culprit for performance issue. So we want to cache the concept server side to solve this performance issue. Can you suggest the best way to approach this problem ?

You can use a cache library like EHCache (the one used by Hibernate) but the real problem is to update it whenever a concept is created/updated/deleted. I guess it needs changes in core.

There is Context.updateSearchIndex() which can be used to rebuild the whole index once a concept is created/updated/deleted. I think the key would be making it more efficient for updating a single concept that has changed.

I thought it was a cache by PK, not the Lucene index. If it’s the latter then I wouldn’t use any cache library because Lucene indexes in theory are superfast.

Do you want to cache the concept between web requests or in a single request?

If the latter then you could fetch your concept by id and that way you will use Hibernate first level cache and avoid hitting db. The cache will not be used when you fetch by uuid or any other query.

For cross web requests cache we don’t have yet a general purpose solution. We’ve experiemented with Spring’s @Cacheable at https://issues.openmrs.org/browse/TRUNK-3675, but it will not be available until after platform 2.1 is out.

For now, if you need a cache other than Hibernate’s first level cache then I’d advise to create your own service and do caching in any way, which works for you. See for example https://github.com/openmrs/openmrs-module-openconceptlab/blob/master/api/src/main/java/org/openmrs/module/openconceptlab/CacheService.java

As @lluismf pointed out you will need to consider what invalidation policy will work in your case. If you do not expect users to edit concepts and you will update concepts only in modules activators when the system starts, then it may be fine to invalidate the cache manually after updates.

1 Like

Just remember that it is not just the concert names which might change. Anything connected to the concept such as new attributes or reference maps.