title: Keep logged user in memory from database - Spring Security
body: <p>I'm writing web app with spring security, I have already default security implementation it works fine, but I have question about getting data from database. How can I keep information from database without executing query to database everytime?</p>
  
  <p>Look at this. User is my entity class, but for me it is not effective to retrive data from database everytime. Everytime I refresh that <code>/welcome</code> it will execute query, I'm using Spring data jpa so it's fine but does not make sense since there is no change in database. So what I want to do is to keep user and retrive his data from db once in my app. Is there any way to do it?</p>
  
  <pre><code>  @RequestMapping(value = "/userpanel")
      public String userpanel(Model model, Principal principal){
  
          String loggedUserName = principal.getName();
          Optional&lt;User&gt; user = userService.findByUserName(loggedUserName);
          if(user.isPresent()){
              model.addAttribute("user", user.get());
          }
  
          return "userpanel";
      }
  </code></pre>