Flyweight Examples:

Java
Java
C#
C#
Python
Python
TypeScript
TypeScript
▸ Flyweight Quick Review

HTTP Request Caching in Java

If you're wanting to save on time and bandwidth, caching HTTP requests for similar responses is a good way to do so, as long as you have an expiration date on how long you can cache a result for; nobody want's last year's information today!

For simplicity, we won't be making actual HTTP requests, and our system will be designed as a concurrent solution, but there wouldn't need to be a lot done to convert these to ready-to-go solutions!

An HTTP Request Cache is a great example of the Flyweight pattern in action; the request response is relatively expensive and time-consuming to acquire, so storing it away where it can be quickly accessed would save on a lot of resources!

Flyweight Object:

public class WebRequestResult
{
   // The time (in minutes, converted to milliseconds) to cahce a request:
   // We're going to cache for 10 minutes:
   public static final long CACHE_TIME_MINUTES = 10 * 60 * 1000;

   // The Flyweight data:
   public String data;
   
   private final long cacheUntilMilli

   public WebRequestResult(String data)
   {
       this.data = data;
       this.cacheUntilMilli = System.currentTimeMillis() + CACHE_TIME_MINUTES;
   }

   // Is the result still considered "fresh"?
   // We want it to "expire" after 10 minutes:
   public boolean IsExpired()
   {
       return System.currentTimeMillis() > this.cacheUntilMilli;
   }
}

WebRequestCache Factory:

public class WebRequestCache
{
   // The cache for the request system:
   static HashMap<String, WebRequestResult> cache = new HashMap<String, WebRequestResult>();

   // We're going to make this simple and be concurrent, even though the world of 
   // web communication would be asynchronous!

   public static WebRequestResult GetRequest(String url)
   {
       WebRequestResult result = null;
       if(cache.containsKey(url))
       {
           result = cache.get(url);
       }

       // If the result is not cached, or is old, replace it:
       if(result == null || result.IsExpired())
       {
           result = new WebRequestResult("Some cool data from " + url);
           // Replace in the cache:
           cache.put(url, result);
       }
       
       // Give back the result:
       return result;
   }
}

Demo:

public class Solution
{
   public static void main(String[] args)
   {
       WebRequestResult result = null;

       result = WebRequestCache.GetRequest("myapiservice/link/endpoint");

       System.out.println(result.data); // Some cool data from myapiservice/link/endpoint

       result = WebRequestCache.GetRequest("myotherservice/endpoint");

       System.out.println(result.data); // Some cool data from myotherservice/endpoint

       System.out.println(result.IsExpired()); // false

       // Sleep for the cache time (WARNING: 10 minutes!!)
       Thread.sleep(result.CACHE_TIME_MINUTES);

       System.out.println(result.IsExpired()); // true
   }
}

Find any bugs in the code? let us know!