Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:52:54+00:00 2026-06-09T03:52:54+00:00

I have an @Cacheable annotated method in one of my beans, and I’d like

  • 0

I have an @Cacheable annotated method in one of my beans, and I’d like to use the currently logged in user ID as the key for the Cache. However, I’m using Spring Security and have an Injected service as an instance variable in this bean that calls SecurityContextHolder.getContext().getAuthentication() to return the user ID. Hence, I have a zero-argument constructor on the @Cacheable method. Is there anyway to use the user ID returned from my injected service’s method as the key for the Cache?

@Service
public class MyServiceImpl implements MyService {

@Inject
private UserContextService userContextService;

@Override
@Cacheable("myCache")
public String getInformation() {
  //use this as the key for the cache entry
String userId = userContextService.getCurrentUser();
return "something";
}
}

UserContextService implementation:

@Service
public class UserContextServiceImpl implements UserContextService {

public String getCurrentUser() {
return SecurityContextHolder.getContext().getAuthentication().getName();
}

}

I found this question, but it’s somewhat different from what I’d like to do. I don’t think this functionality is possible with a static method.

Using Spring beans as a key with @Cacheable annotation

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:52:56+00:00Added an answer on June 9, 2026 at 3:52 am

    I would write this class so that the userId was an argument to the getInformation() method, and make users of the method/service pass lookup the ID themselves.

    @Override
    @Cacheable("myCache")
    public String getInformation(String userId) { ... }
    

    IMO it’s a bad practice to write your service-layer to use the Spring Security context directly, as it limits the usefulness of the service – if you implement some sort of REST API that doesn’t use Spring Security (just as an example), then getCurrentUser() might have no meaning / return value. Then MyServiceImpl is unusable if Spring Security is not used for that request, and this method is unusable if you ever need to support something like “admin user needs to look up information for user X”.

    Let the web-facing layer worry about how to extract the userid from the security context, rather than your service layer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have deployed numerous report parts which reference the same view however one of them
I use Spring 3.0 and ehcache. I have added @Cacheable annotations to some methods
I have a problem with a QueryDSL query. Classes: @Entity @Table(name=project) @Cacheable(true) @Cache(usage= CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
I have problem with the NHibernate's second level cache. When I use query: var
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Google Page Speed says. The following publicly cacheable, compressible resources should have a Vary:
I have the following VCL: sub jamie_fetch { if(beresp.http.X-Var-Cache == YES) { unset beresp.http.Set-Cookie;
I have a situation in a template where I want to use an if
Does anybody know how the default key generation for Ehcache works? If I have
how do i cache specific files in html ? i have tried meta http-equiv=cache-control

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.