Anand Patel
Anand Patel

Reputation: 6421

What is write through cache?

I have implemented caching in the past. The following is the pattern that I used to follow for implementing service layer on top of repositories:

T retrieve(String key)
{
    if (cache.contains(key))
        return cache.get(key)

    T obj = repository.get(key);
    cache.put(key, obj);
}


void create(T object)
{
    repository.create(object);
}


void delete(String key)
{
    cache.remove(key);
    repository.delete(key);
}

Note: Thread safety concerns are not considered in the above pseudocode

I came across this word - write through cache, and want to understand more about it. The following are my questions:

  1. What is write through cache?
  2. How write through cache is different from normal caching?
  3. When write through caching should be used?
  4. How does the pseudocode look like for write through cache?
  5. Are there any other such cache distinctions?

Thanks.

Upvotes: 0

Views: 4425

Answers (2)

AllTooSir
AllTooSir

Reputation: 49352

write through cache- Write-through caching is a caching pattern where writes to the cache cause writes to an underlying resource. The cache acts as a facade to the underlying resource. With this pattern, it often makes sense to read through the cache too. A disk or memory cache that supports the caching of writing. Data written by the CPU to memory or to disk is also written into the cache. Write performance is not improved with this method. However, if a subsequent read operation needs that same data, read performance is improved, because the data are already in the high-speed cache.

Pseudo-code :

void create(T object)
{
    repository.create(object);
    cache.put(somekey, obj);
}

Upvotes: 1

tbsalling
tbsalling

Reputation: 4545

In a write-through cache write is done synchronously to the cache and to the persistent store. In other cache types, the write to persistent storage may be deferred for performance reasons.

Wikipedia describes basic concepts on common cache writing policies.

Upvotes: 4

Related Questions