Skip to content
Advertisement

Understanding Dao-pattern, can it be used for retrieving data from cache?

I read about DAO-pattern in the official documentation and it’s not quite clear if it can be used for retrieving data from cache?

Formally speaking, DAO is an additional abstraction layer between clients and mechanism the data is being retrieved from somewhere. So, if data resides in cache, I suppose we might as well call the DAO something like

public interface UserDao {
    //CRUD operations
}

public class UpdatableCachedUserDaoImpl implements UserDao {
     //Normal dao
     private final UserDao userDao;
     private volatile List<User> cache;

    //Delegates CRUD operation to cache
    //Updates the cache through ScheduledThreadPoolExecutor
    //using the Normal Dao
}

But is it correct to place such cache-related logic in DAO? Or DAO is intended to work precisely with data-source or something else persistent storage?

Advertisement

Answer

Data Access Object pattern doesn’t address what the data is or where it’s being accessed. Whether it makes sense to access a cache from a DAO depends a lot on the other architecture of the system.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement