{
"$type": "site.standard.document",
"canonicalUrl": "https://rednafi.com/python/caching-connection-objects/",
"description": "Learn efficient patterns for caching database connection objects in Python without import-time side effects or lru_cache complexity.",
"path": "/python/caching-connection-objects/",
"publishedAt": "2022-03-16T00:00:00.000Z",
"site": "at://did:plc:fgtm2c26vfcj74rfmeggbyqj/site.standard.publication/3mnl6f7ob462z",
"tags": [
"Python",
"TIL",
"Design Patterns"
],
"textContent": "To avoid instantiating multiple DB connections in Python apps, a common approach is to\ninitialize the connection objects in a module once and then import them everywhere. So,\nyou'd do this:\n\nHowever, this adds import time side effects to your module and can turn out to be expensive.\nIn search of a better solution, my first instinct was to go for functools.lru_cache(None)\nto immortalize the connection objects in memory. It works like this:\n\nThis way, the connection objects returned by the functions are cached and any subsequent\ncalls to the functions will provide the same connection objects from the cache without\nreinitializing them.\n\nOne problem with the above approach is - how complex the implementation of the cache\ndecorator is. Underneath, the functools.cache decorator is an alias for\nfunctools.lru_cache(None) and it employs a Least Recently Used cache eviction policy.\nWhile this policy is quite useful when you need it but to cache simple connection objects,\narguably, the complexity and the overhead of the cache decorator offset its benefits.\nThere's a simpler way to do it and [James Powell on Twitter] pointed me to it. This works as\nfollows:\n\nIs this singleton pattern? Probably so.\n\n\n\n\n[james powell on twitter]:\n https://twitter.com/rednafi/status/1503465791987273729?s=20&t=GlzWHBF_y0ZR-uKHVSP40Q",
"title": "Caching connection objects in Python"
}