Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-introduce Caching #2393

Merged
merged 10 commits into from
Dec 17, 2024
Merged

Re-introduce Caching #2393

merged 10 commits into from
Dec 17, 2024

Conversation

chrisknoll
Copy link
Collaborator

Implemented caching for cohort def list, concept set list, permissions.
Implementation uses ehCache 3.9.
Added test for CohortDefinitionService caching.

Notes:
This implementation diverges from the sandbox: I decided to leverage @CacheEvict on methods instead of JPA Entity Listeners to simplify the implementation.

Currently getting cache info is unprotected, but cache/clear is protected.

Implementation uses ehCache 3.9.
Added test for CohortDefinitionService caching.
Increased size of small-heap configuration.
@anthonysena anthonysena linked an issue Oct 8, 2024 that may be closed by this pull request
Changed asset cache key to use login always due to read/write permissions being assigned at a per-user basis.
@anthonysena anthonysena added this to the v2.15 milestone Nov 22, 2024
Changed editor config to tabs.
Made cache endpoints auth restricted.
@chrisknoll
Copy link
Collaborator Author

chrisknoll commented Dec 6, 2024

@amarsan , would you be willing to do a review on this. I changed one of the functions of the clear cache such that the clear cache button now just clears the WebAPI cache, and the individual 'refresh' buttons next to sources will clear the caches on the specific cdms. We could introduce a button somewhere for refreshing all cdm caches, but I seriously doubt anyone would need that function.

The cache endpints do not have a UI to access them, but they exist so that you can fetch the cache statistics as a rest endpoint. However, this requires passing an auth header into the request so that it passes authetnication. To do this, I access Atlas and go to the console to look at one of the secured WebAPI requets (like source/sources). I look at the header and get the authorization header that looks like this:

Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuYVxcY2tub2xsMSIsIlNlc3Npb24tSUQiOm51bGwsImV4cCI6MTczMzg2NTkzN30.tg_CsfuCv0ah_Id8w8RELtJRvFk1Zq8TghSvXu_x_wVfU1JrGKCbDUiguwu1Cfv62JhEW3c8tvlwr3ljuczzUA

I then use something like postman to make the GET request with the specified header. I think there is also a chrome extension that will let you set headers so that you can make the GET request directly in a browser tab.

I'm just giving you this info in case you wanted to observe the cache statistics changing over time.

}
@CacheEvict(cacheNames = CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
public void invalidateCache() {
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you have @CacheEvict here, but also on the methods in the SourceController that call this method. Is that redundant?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's redundant, but I did have trouble understanding where the line between the service and the controller was being drawn, so there were certain places where cache evict happens at the controller level and the service level in others. In this function, there was a service-level function to invalidate the cache so that's where the evict annotation applies here. I think the reason why it's at the service level is that the service components are typically the ones that are dependency-injected into other components, so if some other component needs to signal a cache invalidation for some reason, it would call out to this service method.

I'm glad you're poking at this because it is revealing some of the inconsistency and coding confusion that we have in the codebase that we could try to address when we have more flexibility to change things (Ie 3.x and 4.x)

@@ -157,6 +157,7 @@ public SourceDetails getSourceDetails(@PathParam("sourceId") Integer sourceId) {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@CacheEvict(cacheNames = SourceService.CachingSetup.SOURCE_LIST_CACHE, allEntries = true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this invalidate the cache even if this is called when security is disabled?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the caching is independent of security being enabled.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so I understand how this CacheEvict works...does it execute after the method executes, and only if it executes without throwing an exception?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fire after the method, and if the method throws an exception, it will not evict.

It can be set to evict the cache before invoking the method by using the beforeInvocation option on the annotation.

In this case, I think we want the default behavior, if it fails to create the source for some reason, then there is no need to cache evict.

@anthonysena anthonysena removed this from the v2.15 milestone Dec 10, 2024
…caching

# Conflicts:
#	src/main/java/org/ohdsi/webapi/service/ConceptSetService.java
@chrisknoll chrisknoll merged commit 70dc4e2 into master Dec 17, 2024
2 checks passed
@chrisknoll chrisknoll deleted the issue-2383-caching branch December 17, 2024 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement Caching for Permissions and asset lists
3 participants