Avi
Avi

Reputation: 21760

Wildcards in cache keys

I have a scenario in which I send 3 parameters to an external service and expect a result. I want to find an elegant way to cache the result.

The issue is that the 3 parameters have hierarchy between them. Something like:

  1. City
  2. Street
  3. Building

I want to be able to cache with wild cards on lower levels than "City". For example:

Using just a plain object with '*' marking wildcards would cause having many "if"s in order to check the conditions. I thought about overriding the equals and the toString methods but I couldn't find the right way to do it.

Upvotes: 0

Views: 175

Answers (1)

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

To be honest, I think that trying to do this as simple strings will be painful to the Nth degree.

I'd recommend a different solution.

Not being a Java developer, I can't give you exact syntax, but the P-code should be close enough.

class Address {

   // Properties.
   City: string,
   Street: string,
   Building: string
}

class Addresses {
    Add: List<Address>

     public bool GetHit(city, street, building) {
       foreach(var add in Add) {
         if (add.city == city) && (add.street == street) && (add.building == building) {
           return true;
         }
       }
       return false;
     }


    public bool GetHitByKey(city, street, building) {
      // First, check to see if there is a wildcard on the street
      if GetHit(city, "*", building) {
        return true;
      }

      if GetHit(city, "*", "*") {
        return true;
      }

      return GetHit(city, street, building);
    }
}

Todo:

  • Pass an object instead of 3 paramaters around
  • Use a binary search rather than linear.

Upvotes: 1

Related Questions