DEV Community

Cover image for Code Smell 49 - Caches
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

1

Code Smell 49 - Caches

Caches are sexy. They are a one-night stand. We need to avoid them in a long-term relationship.

Problems

  • Coupling

  • Testability

  • Cache invalidation.

  • Maintainability

  • Premature Optimization

  • Erratic Behavior

  • Lack of transparency

  • Non-Deterministic behavior

Solutions

  1. If you have a conclusive benchmark and are willing to pay for some coupling: Put an object in the middle.

  2. Unit test all your invalidation scenarios. Experience shows we face them in an incremental way.

  3. Look for a real world cache metaphor and model it.

Sample Code

Wrong

<?
final class Book {
private $cachedBooks;
public function getBooksFromDatabaseByTitle(
string $title) {
if (!isset($this->cachedBooks[$title])) {
$this->cachedBooks[$title] =
$this->doGetBooksFromDatabaseByTitle($title);
}
return $this->cachedBooks[$title];
}
private function doGetBooksFromDatabaseByTitle(
string $title) {
globalDatabase()->selectFrom('Books', 'WHERE TITLE = ' . $title);
}
}

Right

<?
final class Book {
// Just Book related Stuff
}
interface BookRetriever {
public function bookByTitle(string $title);
}
final class DatabaseLibrarian implements BookRetriever {
public function bookByTitle(string $title) {
// Go to the database (not global hopefully)
}
}
final class HotSpotLibrarian implements BookRetriever {
// You always look for real life metaphors
private $inbox;
private $realRetriever;
public function bookByTitle(string $title) {
if ($this->inbox->includesTitle($title)) {
// You are lucky. Someone has just returned the book copy.
return $this->inbox->retrieveAndRemove($title);
} else {
return $this->realRetriever->bookByTitle($title);
}
}
}
view raw notcached.php hosted with ❤ by GitHub

Detection

This is a design smell.

It will be difficult to enforce by policy.

Tags

  • Premature Optimization

Conclusion

Caches should be functional and intelligent

In this way we can manage invalidation.

General purpose caches are suitable only for low level objects like operating systems, files and streams.

We shouldn't cache domain objects.

This page is hosted on a cached website.

Relations

More Info

Common Cache Usages

Caching Patterns

Credits

Photo by Aimee Vogelsang on Unsplash


There are only two hard things in Computer Science: cache invalidation and naming things.

Phil Karlton

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more