Issue with same cacheKey in different caches

22 views
Skip to first unread message

Sweta Nahata

unread,
Jun 5, 2018, 5:10:58 AM6/5/18
to simple-spring-memecached
Hi,

I am using simple spring memcached with spymemcached provider (version 3.3.0) and have defined a number of caches, some of which use the same cache key.

Cache1:
cacheKey -> Value1

Cache2:
cacheKey -> Value2

Since cacheKey is in different caches putting the cacheKey in one cache should not override the one in other cache. However, that is not the case.
This causes ClassCastException when Value1 and Value2 are of different types.

I read in other posts that this issue is resolved in version 3.3.0 of simple-spring-memcached, however i am still observing this issue.

Can you please let me know if this is the expected behaviour?

Thanks,
Sweta

Jakub Białek

unread,
Jun 5, 2018, 6:53:57 AM6/5/18
to simple-spring-memecached
Hi,

Have you set CacheFactory.properties.useNameAsKeyPrefix to true? It should prefix each of the key with a cache's name.

Best regards,
Ragnor

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-spring-memecached@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

Sweta Nahata

unread,
Jun 5, 2018, 9:26:33 AM6/5/18
to simple-spring-memecached
Hi Rangor,

Thanks for the quick response. Yes, I exactly need this, however even though I set useNameAsKeyPrefix to true and the key is not prefixed with cache's name. 
Also, I have upgraded the version to 4.0.0 and still this doesn't work.
Debugging and looking into the code in com.google.code.ssm.CacheImpl in simple-spring-memcached library I don't see the cacheKey prefixed with cache name in the code below.  The CacheImpl takes the CacheProperties in the constructor, however it doesn't use it anywhere.

private <T> void set(String cacheKey, int expiration, T value, SerializationType serializationType, CacheTranscoder cacheTranscoder) throws TimeoutException, CacheException {
if (SerializationType.PROVIDER.equals(serializationType)) {
this.cacheClient.set(cacheKey, expiration, value);
} else if (cacheTranscoder == null) {
throw new IllegalArgumentException(String.format("Cannot use %s serialization because dedicated cache transcoder is null!", serializationType));
} else {
this.cacheClient.set(cacheKey, expiration, value, cacheTranscoder);
}
}

Can you please let me know if i'm missing something here?

Thanks,
Sweta

On Tuesday, June 5, 2018 at 4:23:57 PM UTC+5:30, ragnor84 wrote:
Hi,

Have you set CacheFactory.properties.useNameAsKeyPrefix to true? It should prefix each of the key with a cache's name.

Best regards,
Ragnor
On 5 June 2018 at 11:10, Sweta Nahata <naha...@gmail.com> wrote:
Hi,

I am using simple spring memcached with spymemcached provider (version 3.3.0) and have defined a number of caches, some of which use the same cache key.

Cache1:
cacheKey -> Value1

Cache2:
cacheKey -> Value2

Since cacheKey is in different caches putting the cacheKey in one cache should not override the one in other cache. However, that is not the case.
This causes ClassCastException when Value1 and Value2 are of different types.

I read in other posts that this issue is resolved in version 3.3.0 of simple-spring-memcached, however i am still observing this issue.

Can you please let me know if this is the expected behaviour?

Thanks,
Sweta

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.

Jakub Białek

unread,
Jun 5, 2018, 9:37:19 AM6/5/18
to simple-spring-memecached
Hi,

When the settings is set the factory should  create PrefixedCacheImpl. This cache alters each key and adds a cache's name as a prefix.
Please verify that his cache is created and used and the name is unique.

Best regards,
Ragnor


To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsubscr...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-spring-memecached@googlegroups.com.

Sweta Nahata

unread,
Jun 5, 2018, 11:07:09 AM6/5/18
to simple-spring-memecached
Hi Ragnor,

Below is my configuration

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

<bean class="com.google.code.ssm.CacheFactory" id="openjpa-querycache" parent="defaultCache">
<property name="cacheName" value="openjpa-querycache">
</property>
</bean>

<bean class="com.google.code.ssm.CacheFactory" id="defaultCache">
<property name="cacheName" value="defaultCache"/>
<property name="cacheClientFactory">
<bean class="com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl"></bean>
</property>
<property name="addressProvider">
<bean class="com.google.code.ssm.config.DefaultAddressProvider">
<property name="address" value="localhost:11211">
</property></bean>
</property>
<property name="configuration">
<bean class="com.google.code.ssm.providers.CacheConfiguration">
<property name="consistentHashing" value="true">
</property>
<property name="useBinaryProtocol" value="true" />
<property name="useNameAsKeyPrefix" value="true" />
</bean>
</property>
</bean>

When I debugged the code the else part of the code is called in the below method of CacheFactory which creates an instance of CacheImpl.

protected Cache createCache() throws IOException {
if (this.cache != null) {
throw new IllegalStateException(String.format("This factory has already created memcached client for cache %s", this.cacheName));
} else if (this.isCacheDisabled()) {
LOGGER.warn("Cache {} is disabled", this.cacheName);
this.cache = (Cache)Proxy.newProxyInstance(Cache.class.getClassLoader(), new Class[]{Cache.class}, new DisabledCacheInvocationHandler(this.cacheName, this.cacheAliases));
return this.cache;
} else if (this.configuration == null) {
throw new RuntimeException(String.format("The MemcachedConnectionBean for cache %s must be defined!", this.cacheName));
} else {
List<InetSocketAddress> addrs = this.addressProvider.getAddresses();
this.cache = new CacheImpl(this.cacheName, this.cacheAliases, this.createClient(addrs), this.defaultSerializationType, this.jsonTranscoder, this.javaTranscoder, this.customTranscoder, new CacheProperties(this.configuration.isUseNameAsKeyPrefix(), this.configuration.getKeyPrefixSeparator()));
return this.cache;
}
}

Can you please let me know if i'm missing something here?

Thanks,
Sweta
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.

Jakub Białek

unread,
Jun 6, 2018, 12:51:14 AM6/6/18
to simple-spring-memecached
Hi,

This configuration doesn't work when you use SSM behind Spring Cache as the logic responsible for creating prefixed cache is in CacheBase (it isn't used when access is via Spring Cache).
To make it work you have to wrap cache objects:

<bean class="com.google.code.ssm.spring.SSMCacheManager" id="cacheManager">
<property name="caches">
<set>
<bean class="com.google.code.ssm.spring.SSMCache">
<constructor-arg index="0" name="cache">
                    <bean class="com.google.code.ssm.PrefixedCacheImpl">

<constructor-arg index="0" name="cache" ref="defaultCache"/>
                        <constructor-arg index="1" name="requestedName" value="defaultCache"/>
<constructor-arg index="2" name="namePrefixSeparator" value="#"/>
</bean>

</constructor-arg>
<constructor-arg index="1" name="expiration" value="1"/>
                <constructor-arg index="2" name="allowClear" value="false"/>
            </bean>
<bean class="com.google.code.ssm.spring.SSMCache">
<constructor-arg index="0" name="cache">
                    <bean class="com.google.code.ssm.PrefixedCacheImpl">

<constructor-arg index="0" name="cache" ref="openjpa-querycache"/>
                        <constructor-arg index="1" name="requestedName" value="openjpa-querycache"/>
<constructor-arg index="2" name="namePrefixSeparator" value="#"/>
</bean>

</constructor-arg>
<constructor-arg index="1" name="expiration" value="5"/>
                <constructor-arg index="2" name="allowClear" value="false"/>
            </bean>
</set>
</property>
</bean>
Please let me know if it helps you.

Best regards,
Ragnor






To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsubscr...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsubscr...@googlegroups.com.

To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-spring-memecached@googlegroups.com.

Sweta Nahata

unread,
Jun 6, 2018, 2:56:21 AM6/6/18
to simple-spring-memecached
Thanks a lot Ragnor. This worked.

Thanks,
Sweta
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
To post to this group, send email to simple-sprin...@googlegroups.com.
Visit this group at https://groups.google.com/group/simple-spring-memecached.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "simple-spring-memecached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simple-spring-memecached+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages