1515
1616package com .mc .hibernate .memcached ;
1717
18+ import com .mc .hibernate .memcached .keystrategy .KeyStrategy ;
1819import com .mc .hibernate .memcached .keystrategy .Sha1KeyStrategy ;
20+ import org .slf4j .Logger ;
21+ import org .slf4j .LoggerFactory ;
1922
2023public class Config {
2124
25+ private final Logger log = LoggerFactory .getLogger (Config .class );
26+
2227 public static final String PROP_PREFIX = "hibernate.memcached." ;
2328
2429 private static final String CACHE_TIME_SECONDS = "cacheTimeSeconds" ;
25- public static final String PROP_CACHE_TIME_SECONDS = PROP_PREFIX + CACHE_TIME_SECONDS ;
26-
2730 private static final String CLEAR_SUPPORTED = "clearSupported" ;
28- public static final String PROP_CLEAR_SUPPORTED = PROP_PREFIX + CLEAR_SUPPORTED ;
29-
3031 private static final String MEMCACHE_CLIENT_FACTORY = "memcacheClientFactory" ;
31- public static final String PROP_MEMCACHE_CLIENT_FACTORY = PROP_PREFIX + MEMCACHE_CLIENT_FACTORY ;
32-
3332 private static final String DOGPILE_PREVENTION = "dogpilePrevention" ;
34- public static final String PROP_DOGPILE_PREVENTION = PROP_PREFIX + DOGPILE_PREVENTION ;
35-
3633 private static final String DOGPILE_PREVENTION_EXPIRATION_FACTOR = "dogpilePrevention.expirationFactor" ;
37- public static final String PROP_DOGPILE_PREVENTION_EXPIRATION_FACTOR = PROP_PREFIX + DOGPILE_PREVENTION_EXPIRATION_FACTOR ;
38-
3934 private static final String KEY_STRATEGY = "keyStrategy" ;
4035
41- public static final int DEFAULT_CACHE_TIME_SECONDS = 300 ;
42- public static final boolean DEFAULT_CLEAR_SUPPORTED = false ;
43- public static final boolean DEFAULT_DOGPILE_PREVENTION = false ;
44- public static final String DEFAULT_MEMCACHE_CLIENT_FACTORY = "com.mc.hibernate.memcached.spymemcached.SpyMemcacheClientFactory" ;
36+ private static final int DEFAULT_CACHE_TIME_SECONDS = 300 ;
37+ private static final boolean DEFAULT_CLEAR_SUPPORTED = false ;
38+ private static final boolean DEFAULT_DOGPILE_PREVENTION = false ;
39+ private static final int DEFAULT_DOGPILE_EXPIRATION_FACTOR = 2 ;
40+ private static final String DEFAULT_MEMCACHE_CLIENT_FACTORY = "com.mc.hibernate.memcached.spymemcached.SpyMemcacheClientFactory" ;
4541
4642 private PropertiesHelper props ;
47- private static final int DEFAULT_DOGPILE_EXPIRATION_FACTOR = 2 ;
4843
4944 public Config (PropertiesHelper props ) {
5045 this .props = props ;
5146 }
5247
5348 public int getCacheTimeSeconds (String cacheRegion ) {
54- int globalCacheTimeSeconds = props .getInt (PROP_CACHE_TIME_SECONDS ,
55- DEFAULT_CACHE_TIME_SECONDS );
56- return props .getInt (cacheRegionPrefix (cacheRegion ) + CACHE_TIME_SECONDS ,
57- globalCacheTimeSeconds );
49+ int globalCacheTimeSeconds = props .getInt (PROP_PREFIX + CACHE_TIME_SECONDS , DEFAULT_CACHE_TIME_SECONDS );
50+ return props .getInt (cacheRegionPrefix (cacheRegion ) + CACHE_TIME_SECONDS , globalCacheTimeSeconds );
5851 }
5952
6053 public String getKeyStrategyName (String cacheRegion ) {
61- String globalKeyStrategy = props .get (PROP_PREFIX + KEY_STRATEGY ,
62- Sha1KeyStrategy .class .getName ());
54+ String globalKeyStrategy = props .get (PROP_PREFIX + KEY_STRATEGY , Sha1KeyStrategy .class .getName ());
6355 return props .get (cacheRegionPrefix (cacheRegion ) + KEY_STRATEGY , globalKeyStrategy );
6456 }
6557
58+ private KeyStrategy instantiateKeyStrategy (String cls ) {
59+ try {
60+ return (KeyStrategy ) Class .forName (cls ).newInstance ();
61+ } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e ) {
62+ log .warn ("Could not instantiate keyStrategy class " + cls + ". Will use default: Sha1KeyStrategy" , e );
63+ }
64+ return new Sha1KeyStrategy ();
65+ }
66+
67+ public KeyStrategy getKeyStrategy (String cacheRegion ) {
68+ String strategyClassName = getKeyStrategyName (cacheRegion );
69+ return instantiateKeyStrategy (strategyClassName );
70+ }
71+
6672 public boolean isClearSupported (String cacheRegion ) {
67- boolean globalClearSupported = props .getBoolean (PROP_CLEAR_SUPPORTED ,
68- DEFAULT_CLEAR_SUPPORTED );
69- return props .getBoolean (cacheRegionPrefix (cacheRegion ) + CLEAR_SUPPORTED ,
70- globalClearSupported );
73+ boolean globalClearSupported = props .getBoolean (PROP_PREFIX + CLEAR_SUPPORTED , DEFAULT_CLEAR_SUPPORTED );
74+ return props .getBoolean (cacheRegionPrefix (cacheRegion ) + CLEAR_SUPPORTED , globalClearSupported );
7175 }
7276
7377 public boolean isDogpilePreventionEnabled (String cacheRegion ) {
74- boolean globalDogpilePrevention = props .getBoolean (PROP_DOGPILE_PREVENTION ,
75- DEFAULT_DOGPILE_PREVENTION );
76- return props .getBoolean (cacheRegionPrefix (cacheRegion ) + DOGPILE_PREVENTION ,
77- globalDogpilePrevention );
78+ boolean globalDogpilePrevention = props .getBoolean (PROP_PREFIX + DOGPILE_PREVENTION , DEFAULT_DOGPILE_PREVENTION );
79+ return props .getBoolean (cacheRegionPrefix (cacheRegion ) + DOGPILE_PREVENTION , globalDogpilePrevention );
7880 }
7981
8082 public double getDogpilePreventionExpirationFactor (String cacheRegion ) {
81- double globalFactor = props .getDouble (PROP_DOGPILE_PREVENTION_EXPIRATION_FACTOR ,
82- DEFAULT_DOGPILE_EXPIRATION_FACTOR );
83- return props .getDouble (cacheRegionPrefix (cacheRegion ) + DOGPILE_PREVENTION_EXPIRATION_FACTOR ,
84- globalFactor );
83+ double globalFactor = props .getDouble (PROP_PREFIX + DOGPILE_PREVENTION_EXPIRATION_FACTOR , DEFAULT_DOGPILE_EXPIRATION_FACTOR );
84+ return props .getDouble (cacheRegionPrefix (cacheRegion ) + DOGPILE_PREVENTION_EXPIRATION_FACTOR , globalFactor );
8585 }
8686
8787 public String getMemcachedClientFactoryName () {
88- return props .get (PROP_MEMCACHE_CLIENT_FACTORY ,
89- DEFAULT_MEMCACHE_CLIENT_FACTORY );
88+ return props .get (PROP_PREFIX + MEMCACHE_CLIENT_FACTORY , DEFAULT_MEMCACHE_CLIENT_FACTORY );
9089 }
9190
9291 private String cacheRegionPrefix (String cacheRegion ) {
@@ -96,4 +95,4 @@ private String cacheRegionPrefix(String cacheRegion) {
9695 public PropertiesHelper getPropertiesHelper () {
9796 return props ;
9897 }
99- }
98+ }
0 commit comments