001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.cache.interceptor; 018 019import java.lang.reflect.Method; 020 021/** 022 * Simple key generator. Returns the parameter itself if a single non-null 023 * value is given, otherwise returns a {@link SimpleKey} of the parameters. 024 * 025 * <p>No collisions will occur with the keys generated by this class. 026 * The returned {@link SimpleKey} object can be safely used with a 027 * {@link org.springframework.cache.concurrent.ConcurrentMapCache}, however, 028 * might not be suitable for all {@link org.springframework.cache.Cache} 029 * implementations. 030 * 031 * @author Phillip Webb 032 * @author Juergen Hoeller 033 * @since 4.0 034 * @see SimpleKey 035 * @see org.springframework.cache.annotation.CachingConfigurer 036 */ 037public class SimpleKeyGenerator implements KeyGenerator { 038 039 @Override 040 public Object generate(Object target, Method method, Object... params) { 041 return generateKey(params); 042 } 043 044 /** 045 * Generate a key based on the specified parameters. 046 */ 047 public static Object generateKey(Object... params) { 048 if (params.length == 0) { 049 return SimpleKey.EMPTY; 050 } 051 if (params.length == 1) { 052 Object param = params[0]; 053 if (param != null && !param.getClass().isArray()) { 054 return param; 055 } 056 } 057 return new SimpleKey(params); 058 } 059 060}