AArhin / simple-spring-memcached

Automatically exported from code.google.com/p/simple-spring-memcached
MIT License
0 stars 0 forks source link

spring-cache cacheAlias(cache zone) support. #21

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
spring-cache does not support cache aliases(cache zones).

spring-cache SSMCache generate cache key without any information about cache 
zones. So I had to add prefix for all the keys.

Because of that I add cacheAliases and a key generator support for spring-cache.

For example when you have the following settings,

<bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
    <property name="caches">
        <set>
            <bean class="com.google.code.ssm.spring.SSMCache">
                <constructor-arg name="cache" index="0" ref="userCache" />
                <constructor-arg name="expiration" index="1" value="300" />
                <constructor-arg name="allowClear" index="2" value="false" />
            </bean>
        </set>
    </property>
</bean>

<bean name="userCache" class="com.google.code.ssm.CacheFactory">
    <property name="cacheName" value="userCache" />
            .....
    <property name="cacheAliases">
        <set>
            <value>userAliasCache1</value>
            <value>userAliasCache2</value>
        </set>
    </property>
</bean>

You can use, userCache, userAliasCache1, userAliasCache2 as 
@Cacheable#value,@CachePut#value, @CacheEvict#value, ....

When you have the following method and both entity.id and secondEneity.id have 
the same value(ex, 1).

@Cacheable(value="userCache", key="#entity.id")
public Entity get() { .. }

@Cacheable(value="userAliasCache1", key="#secondEntity.id")
public SecondEntity get() { .. }

memcached will have "userCache::1" and "userAliasCache1::1" keys.

Of course you can change generated key formats with 
com.google.code.ssm.spring.SSMKeyGenerator implmentations.

Original issue reported on code.google.com by kwon3...@gmail.com on 3 Oct 2013 at 1:41

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for the patch!
Adding cache name or alias as a prefix to key can be useful to avoid keys' 
collision. I haven't got time to review your patch but it seams that it will 
always prefix keys. It will break backward compatibility with previous version 
of SSM - different cache key will be generated for the same object by current a 
new version of SSM. I think that key prefixing should be optional and by 
default disabled to keep backward compatibility.

Original comment by ragno...@gmail.com on 4 Oct 2013 at 6:00

GoogleCodeExporter commented 9 years ago
You're right. cache name or alias are always prefix cache key.
So I made SSMKeyGenerator interface.
If you change SSMKeyGenerator, you can disable prefixing key.
But I made default SSMKeyGenerator to prefix cache name/alias.

Original comment by kwon3...@gmail.com on 4 Oct 2013 at 6:12

GoogleCodeExporter commented 9 years ago
Base on your patch I've implemented it in a slightly different way. A new class 
PrefixedCacheImpl is used to wrap all requests and alter cache key before 
invoking CacheImpl. By default PrefixedCacheImpl is not used.
To enable it:

<bean name="userCache" class="com.google.code.ssm.CacheFactory">
    <property name="cacheName" value="userCache" />
            .....
    <property name="configuration">
       <bean class="com.google.code.ssm.providers.CacheConfiguration">
            ........
        <property name="useNameAsKeyPrefix" value="true" />
    </bean>
    </property>
    <property name="cacheAliases">
        <set>
            <value>userAliasCache1</value>
            <value>userAliasCache2</value>
        </set>
    </property>
</bean>

and if you want to make the cache available in Spring under aliases:
<bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
    <property name="caches">
        <set>
            <bean class="com.google.code.ssm.spring.SSMCache">
                <constructor-arg name="cache" index="0" ref="userCache" />
                <constructor-arg name="expiration" index="1" value="300" />
                <constructor-arg name="allowClear" index="2" value="false" />
                <constructor-arg name="registerAliases" index="3" value="true" />
            </bean>
        </set>
    </property>
</bean>

Using above configuration you can use: userCache, userAliasCache1, 
userAliasCache2 as a value in @Cacheable and depends on which value is used 
cache keys will be prefixed with: userCache#, userAliasCache1#, userAliasCache2#

Original comment by ragno...@gmail.com on 5 Dec 2013 at 12:28

GoogleCodeExporter commented 9 years ago
Code in trunk.

Original comment by ragno...@gmail.com on 5 Dec 2013 at 12:31

GoogleCodeExporter commented 9 years ago
Oh, does it mean "cache name prefix key" is available for both the spring cache 
abstraction and the original Simple Spring Memcached?
What a nice patch! That is the thing I really wanted.
Thanks!

Original comment by kwon3...@gmail.com on 8 Dec 2013 at 12:42

GoogleCodeExporter commented 9 years ago
Yes, it does :). 
You can configure the library to use cache name/alias as a prefix for key in 
the original SSM and Spring Cache abstraction. 

Original comment by ragno...@gmail.com on 8 Dec 2013 at 6:42

GoogleCodeExporter commented 9 years ago
Included in release 3.3.0. The release is available in central maven 
repository. 

Original comment by ragno...@gmail.com on 17 Jan 2014 at 6:17

GoogleCodeExporter commented 9 years ago
Thanks for the fix, exactly what I needed!

Original comment by reinhard...@gmail.com on 24 Mar 2014 at 2:27