/**
 * 
 */
package com.trendrr.oss.cache;

import java.io.UnsupportedEncodingException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.trendrr.oss.StringHelper;


/**
 * @author Dustin Norlander
 * @created Oct 9, 2012
 * 
 */
public class DefaultTrendrrCacheKeyMaker implements TrendrrCacheKeyMaker {

	protected static Log log = LogFactory
			.getLog(DefaultTrendrrCacheKeyMaker.class);

	/* (non-Javadoc)
	 * @see com.trendrr.oss.cache.TrendrrCacheKeyMaker#toKey(java.lang.String, java.lang.String)
	 */
	@Override
	public String toKey(String namespace, String key) {
		if (namespace != null) {
			key = namespace + key;
		}
		if (key.length() > 24) {
			try {
				key = StringHelper.sha1Hex(key.getBytes("utf8"));
			} catch (UnsupportedEncodingException e) {
				log.warn("Invalid key: " + key, e);
			}
		} 
//		log.info("key: " + key);
		return key;
	}
}

@@ -434,17 +434,128 @@
 class);%0A
+%09%0A%09/**%0A%09 * we sha1 hash any keys longer then this.%0A%09 */%0A%09public static int MAX_KEY_LENGTH_BEFORE_HASH = 24;%0A%09%0A%09
 %0A
-
 %09/* (non
@@ -794,25 +794,26 @@
 if (
-key.length() %3E 24
+shouldHashKey(key)
 ) %7B%0A
@@ -975,36 +975,451 @@
 %09%7D %0A
-//%09%09log.info(%22key: %22 + key);
+%09%09return key;%0A%09%7D%0A%09%0A%09%0A%09protected static boolean shouldHashKey(String key) %7B%0A%09%09try %7B%0A%09%09%09byte%5B%5D keyBytes = key.getBytes(%22utf8%22);%0A%09%09%09// Validate the key%0A%09%09%09if (key.length()%3E MAX_KEY_LENGTH_BEFORE_HASH) %7B%0A%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%09for (byte b : keyBytes) %7B%0A%09%09      if (b == ' ' %7C%7C b == '%5Cn' %7C%7C b == '%5Cr' %7C%7C b == 0) %7B%0A%09%09    %09  return true;%0A%09%09      %7D%0A%09%09    %7D%0A%09%09%09return false;%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09log.error(%22Caught%22, e);%0A%09%09%7D
 %0A%09%09r
@@ -1420,22 +1420,26 @@
 %09%7D%0A%09%09return 
-key;
+true;   
 %0A%09%7D%0A%7D%0A

better keymaker. hash if any invalid chars are present in the key