Cache logic made Generic

Now it will be easier to implement differnt types of Cache logic with
minimal changes in the code.