一、前言
关于spring-context包org.springframework.cache.interceptor.SimpleKey、org.springframework.cache.interceptor.SimpleKeyGenerator简单键值生成器,基于Arrays.deepHashCode获取数组hashcode作为键值key,并定义空数组对象SimpleKey.EMPTY,详情参见源码说明部分。
二、源码说明
1. SimpleKey类
package org.springframework.cache.interceptor;@b@@b@import java.io.Serializable;@b@import java.util.Arrays;@b@import org.springframework.util.Assert;@b@import org.springframework.util.StringUtils;@b@@b@public class SimpleKey@b@ implements Serializable@b@{@b@ public static final SimpleKey EMPTY = new SimpleKey(new Object[0]);@b@ private final Object[] params;@b@ private final int hashCode;@b@@b@ public SimpleKey(Object[] elements)@b@ {@b@ Assert.notNull(elements, "Elements must not be null");@b@ this.params = new Object[elements.length];@b@ System.arraycopy(elements, 0, this.params, 0, elements.length);@b@ this.hashCode = Arrays.deepHashCode(this.params);@b@ }@b@@b@ public boolean equals(Object obj)@b@ {@b@ return ((this == obj) || ((obj instanceof SimpleKey) && @b@ (Arrays.deepEquals(this.params, ((SimpleKey)obj).params))));@b@ }@b@@b@ public final int hashCode()@b@ {@b@ return this.hashCode;@b@ }@b@@b@ public String toString()@b@ {@b@ return super.getClass().getSimpleName() + " [" + StringUtils.arrayToCommaDelimitedString(this.params) + "]";@b@ }@b@}
2.SimpleKeyGenerator类
package org.springframework.cache.interceptor;@b@@b@import java.lang.reflect.Method;@b@@b@public class SimpleKeyGenerator@b@ implements KeyGenerator@b@{@b@ public Object generate(Object target, Method method, Object[] params)@b@ {@b@ return generateKey(params);@b@ }@b@@b@ public static Object generateKey(Object[] params)@b@ {@b@ if (params.length == 0)@b@ return SimpleKey.EMPTY;@b@@b@ if (params.length == 1) {@b@ Object param = params[0];@b@ if ((param != null) && (!(param.getClass().isArray())))@b@ return param;@b@ }@b@@b@ return new SimpleKey(params);@b@ }@b@}