Last active
          August 29, 2015 14:10 
        
      - 
      
- 
        Save ondrej-kvasnovsky/d4ee54eb8df095989506 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package com.company.cache | |
| import com.google.common.base.Charsets | |
| import com.google.common.hash.HashFunction | |
| import com.google.common.hash.Hasher | |
| import com.google.common.hash.Hashing | |
| import com.vendavo.security.pseudo.SecUser | |
| import org.springframework.cache.interceptor.KeyGenerator | |
| import org.springframework.security.core.Authentication | |
| import org.springframework.security.core.context.SecurityContextHolder | |
| import java.lang.reflect.Method | |
| class CustomKeyGenerator implements KeyGenerator { | |
| @Override | |
| Object generate(Object target, Method method, Object... params) { | |
| HashFunction hf = Hashing.sha512() | |
| Hasher hasher = hf.newHasher().putInt(target.hashCode()) | |
| hasher.putString(method.returnType.canonicalName, Charsets.UTF_8) | |
| hasher.putString(method.declaringClass.canonicalName, Charsets.UTF_8) | |
| hasher.putString(method.parameterTypes.collect { it.canonicalName }.join(','), Charsets.UTF_8) | |
| params.findAll { it }.each { hasher.putInt(it.hashCode()) } | |
| Authentication authentication = SecurityContextHolder.context?.authentication | |
| if (authentication && authentication.principal) { | |
| // or get whetever parameter from security context principal... | |
| hasher.putInt(authentication.principal.hashCode()) | |
| } | |
| return hasher.hash().asLong() | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <?xml version="1.0" encoding="UTF-8"?> | |
| <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xmlns:cache="http://www.springframework.org/schema/cache" xmlns:aop="http://www.springframework.org/schema/aop" | |
| xsi:schemaLocation="http://www.springframework.org/schema/beans | |
| http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" | |
| profile="cache"> | |
| <cache:annotation-driven key-generator="keyGenerator"/> | |
| <bean id="keyGenerator" class="com.company.cache.TenantKeyGenerator"/> | |
| <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> | |
| <property name="caches"> | |
| <set> | |
| <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> | |
| <property name="name" value="default"/> | |
| </bean> | |
| </set> | |
| </property> | |
| </bean> | |
| </beans> | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
More about configuration: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/EnableCaching.html