Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
This PR restructures the code in
app/caches.pyto implement a bridge pattern by separating the cache interface from its implementation, so that either can be varied separately without changing the client (location service) code. To do this, I created a new interface (abstract base class) calledCachesin the caches.py file, with definitions for abstract methods to get, check, and load the cache, as well as a property which stores the instantiation of the cache. Each of these method and property definitions don't do anything in the abstract base class, but simply raiseNotImplementedError.Then, I created two concrete implementations of the interface through the
RedisCacheandSimpleMemoryCacheclasses. Each implement the abstract methods defined in the base class, but use a redis-based cache and simple memory cache, respectively. I also needed to change some of the code inapp/data/__init__.pyto read the project settings and instantiate the proper cache to be used by each of the location services, as well as altered the location services themselves to use theCachesabstract base class in their cache operations. By doing things in this way, any cache implementation can be passed into any of the location services, and this can even be changed dynamically at runtime.Why make these changes?
Since this part of the codebase contained the pattern of having the location services, which were implicitly combined with some cache variation, it made sense to de-couple the two via the bridge pattern. In this example of the pattern, the location services serve as the abstraction/refined abstractions, while the Caches abstract base class is the implementation, with the actual variations of the cache (RedisCache and SimpleMemoryCache) being the concrete implementations.
Doing this makes it possible to use any cache variation with any of the location services, without the need for individual subclasses for all the combinations of cache and location service. Using a bridge pattern here also makes it possible to introduce new location services and new cache variations independently from each other, since our code now only works with high-level abstractions. This change also breaks down the somewhat complex and monolithic
app/caches.pyfile, and separates the code that decides on the cache to use from the actual implementation of the cache, thereby making the code more readable.