Débora
Débora

Reputation: 5952

Cache Regions and Cache memory Groups: Memcache, EhCache, JCS

Most of caching service support to cache any object as key and value pair. But I am looking for a way that objects can be cached as regions or groups. That means, caching objects should be able to group. One or more objects are cached to a group. What ever we do (remove, update) to the group, should be affected to the objects in the group.

I searched about some caching services and found that JCS supports this. I am supposed to implement a common way as it could be used for most of caching services. Does anyone know any way or supportive resource that can be helpful?

Edit: @ cruftex: Assume an Inventory system for small shop. There are stationery items, vegetables, fruits, sweets and their prices and other details are required to be cached. What if I need to clear or do any update only for items that can be eaten (vegetable, fruits, sweets) and not to stationery and any other inedible set of objects that are cached? If there is a way to group like 'edible', 'inedible', I can get or do any changes to the particular group so that all group member instances will be affected.

Upvotes: 1

Views: 1134

Answers (1)

cruftex
cruftex

Reputation: 5723

For the moment I cannot see that your usage scenario justifies a special cache feature.

  1. If you update your products in a write through configuration via Cache.put(), the cache is automatically in sync.

  2. If you use a persistence layer e.g. JPA, its internal cache is in sync also.

  3. If you are in charge of controlling the cache yourself, you just need to know what objects belong to a group and do the invalidation, e.g.:

    Cache<Integer,Set<Integer>> groupId2productsId = ....
    Cache<Integer, Product> productId2product = ....
    
    void invalidateGroup(int id) {
      productId2product.removeAll(groupId2productsId.get(id));
    }
    
  4. You can, of course, partition your products into multiple caches (or regions). Then I would do a partition algorithm on top of a normal cache. But this is an unflexible approach, if you have more then one partition criteria or you have overlaps, then you are in trouble.

General note: For addressing a group of objects by a criteria you need an index structure, or iterate through everything. Typically this is the job of a database. EHCache has a search feature build in, so you can query objects within your cache and invalidate them, but I doubt that EHCache has a fast/indexed implementation. I will think about whether it makes sense to put something like this in a cache, however it won't be a "standards" based solution.

Upvotes: 2

Related Questions