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 7442529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:11:15+00:00 2026-05-29T11:11:15+00:00

When should I reuse a cache in Ehcache and when should I create a

  • 0

When should I reuse a cache in Ehcache and when should I create a new one?

Example 1:

I have the following methods:

public Dog getBestDog(String name) {
    //Find the best dog with the provided name
}

public Dog getBestBrownDog(String name) {
    //Find the best brown dog with the provided name
}

For a given String (e.g. “rover”), these two methods could return a different Dog object.

Should I annotate them both with @Cacheable(cacheName = "dogs") or should I put them in two different caches, “bestDogs” and “bestBrownDogs”?

Example 2:

I have the following methods:

public Dog getBestDogByName(String name) {
    //Find the best dog with the provided name
}

public Dog getBestDogByColour(String colour) {
    //Find the best dog with the provided colour
}

Name “rover” and colour “doggy-colour” could return the same Dog.

Should I annotate them both with @Cacheable(cacheName = "dogs") or should I put them in two different caches, ‘dogsByName’ and ‘dogsByColour’?

  • 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-05-29T11:11:16+00:00Added an answer on May 29, 2026 at 11:11 am

    Whenever you have a scenario where the same key can result in different results, then you probably want a separate cache.

    Example 1:

    getBestDog(name) – use the name as the key from ‘best-dogs’ cache

    getBestBrownDog(name) – use the name as the key from ‘best-brown-dogs’ cache

    Example 2:

    getBestDogByName(name) – same as example 1, use name as key from ‘best-dogs’ cache

    getBestDogByColour(colour) – use colour as key from ‘best-dogs-by-colour’ cache

    Which leaves you with 3 caches, ‘best-dogs’, ‘best-brown-dogs’, ‘best-dogs-by-colour’

    In theory, you could merge ‘best-dogs’ and ‘best-dogs-by-colour’… but maybe you have a dog who is called ‘red’.. so that would be an unaccounted for edge case.

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

Sidebar

Related Questions

This should be fairly simple question. I'm using DocX library to create new word
I've read that i should create and reuse delegates to get the value of
I need to create a new website, I have the database modeling done and
When should I use one versus the other? I want to cache a certain
I have a few questions about Core Location. 1) Should the user refuse permission
Should the folders in a solution match the namespace? In one of my teams
Should I catch exceptions for logging purposes? public foo(..) { try { ... }
What's the proper way for other people to reuse my Activities? Should they hard
I am new to JQuery and need som direction. I have created a page
We all know that we should rather reuse a JDBC PreparedStatement than creating a

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.