Flyweight Examples:

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

HTTP Request Caching in Python

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:

import time

# HTTP Request result:
class WebRequestResult(object):
   # The time (in munutes, converted to seconds) to cache a request.
   # This is a 1-minute cahce.
   CACHE_TIME_MINUTES = 1 * 60 

   def __init__(self, data):
       # The Flyweight data:
       self.data = data
       self.cacheUntilTime = time.time() + WebRequestResult.CACHE_TIME_MINUTES

   def isExpired(self):
       return time.time() > self.cacheUntilTime
   

WebRequestCache Factory:

class WebRequestCache(object):
   def __init__(self):
       self.cache = {}

   # For simplicity, we'll be concurrent with our requests:
   def getRequest(self, url):
       result = None

       if url in self.cache:
           result = self.cache[url]

       if result is None or result.isExpired():
           result = WebRequestResult("Some cool data from " + str(url))

           self.cache[url] = result
       
       return result

Demo:

webCache = WebRequestCache()

request = webCache.getRequest("mycoolapi/endpoint")

print(request.data) # output: Some cool data from mycoolapi/endpoint

print(request.isExpired()) # output: False

# Will wait for 1 minute!
time.sleep(WebRequestResult.CACHE_TIME_MINUTES)

print(request.isExpired()) # output: True

Find any bugs in the code? let us know!