lundi 26 octobre 2020

How to name Cache-Aside function according to CQS principle

For example, I have a function that retrieves data from cache or if the cache is empty, the function gets the result from DB or third-party API, and before returning, store the result to the cache (it looks like a primitive example of the Cache-Aside pattern).

public function getProductById(string $id): IProduct
{
    try {
        return $this->cache->getProductById($id);
    } catch (ProductDoesntExistException $e) {
        $product = $this->db->getProductById($id);
        $this->cache->saveProduct($product);

        return $product;
    }
}

So, according to the Command–query separation principle, we shouldn't do like this, but, let it be an exception. When seeing the name like getSometing we expect that we don't change anything, but here we actually save cache. So, what is the best way of naming functions like this?

Aucun commentaire:

Enregistrer un commentaire