What is the best way to cache OpenMRS Concept server side

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