All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.redis.cache.RedisCacheAdapter Maven / Gradle / Ivy

There is a newer version: 2.0.2.0
Show newest version
/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.data.redis.cache;

import java.lang.reflect.Field;

import org.springframework.data.redis.core.RedisOperations;
import org.springframework.util.ReflectionUtils;

import lombok.NonNull;

/**
 * {@link RedisCache} adapter
 */
public abstract class RedisCacheAdapter extends RedisCache { // @checkstyle:ok
	
	/**
	 * Constructor
	 * 
	 * @param source {@link RedisCache}
	 */
	public RedisCacheAdapter(RedisCache source) {
		
		super(source.getName(), getKeyPrefix(source), (RedisOperations) source.getNativeCache(),
			getExpiration(source));
	}
	
	/**
	 * Constructor
	 * 
	 * @param name cache name
	 * @param prefix cache key prefix
	 * @param redisOperations {@link RedisOperations}
	 * @param expiration expiration
	 */
	public RedisCacheAdapter(String name, byte[] prefix, RedisOperations redisOperations, long expiration) {
		
		super(name, prefix, redisOperations, expiration);
	}
	
	/**
	 * Get cache key prefix
	 * 
	 * @param cache {@link RedisCache}
	 * @return cache key prefix
	 */
	public static byte[] getKeyPrefix(RedisCache cache) {
		
		return getMetadata(cache).getKeyPrefix();
	}
	
	/**
	 * Get expiration
	 * 
	 * @param cache {@link RedisCache}
	 * @return expiration
	 */
	public static long getExpiration(RedisCache cache) {
		
		return getMetadata(cache).getDefaultExpiration();
	}
	
	/**
	 * Get {@link org.springframework.data.redis.cache.RedisCache.RedisCacheMetadata}
	 * 
	 * @param cache {@link RedisCache}
	 * @return {@link org.springframework.data.redis.cache.RedisCache.RedisCacheMetadata}
	 */
	public static RedisCacheMetadata getMetadata(RedisCache cache) {
		
		Field field = ReflectionUtils.findField(RedisCache.class, "cacheMetadata");
		ReflectionUtils.makeAccessible(field);
		
		return (RedisCacheMetadata) ReflectionUtils.getField(field, cache);
	}
	
	/**
	 * Get {@link CacheValueAccessorAdapter}
	 * 
	 * @param cache {@link RedisCache}
	 * @return {@link CacheValueAccessorAdapter}
	 */
	public static CacheValueAccessorAdapter getCacheValueAccessor(RedisCache cache) {
		
		Field field = ReflectionUtils.findField(RedisCache.class, "cacheValueAccessor");
		ReflectionUtils.makeAccessible(field);
		
		return new CacheValueAccessorAdapter((CacheValueAccessor) ReflectionUtils.getField(field, cache));
	}
	
	/**
	 * {@link org.springframework.data.redis.cache.RedisCache.CacheValueAccessor} adapter
	 */
	public static class CacheValueAccessorAdapter extends CacheValueAccessor {
		
		/**
		 * {@link org.springframework.data.redis.cache.RedisCache.CacheValueAccessor}
		 */
		private CacheValueAccessor delegate;
		
		/**
		 * Constructor
		 * 
		 * @param delegate {@link org.springframework.data.redis.cache.RedisCache.CacheValueAccessor}
		 */
		public CacheValueAccessorAdapter(@NonNull CacheValueAccessor delegate) {
			
			super(null);
			
			this.delegate = delegate;
		}
		
		@Override
		public byte[] convertToBytesIfNecessary(Object value) {
			
			return this.delegate.convertToBytesIfNecessary(value);
		}
		
		@Override
		public Object deserializeIfNecessary(byte[] value) {
			
			return this.delegate.deserializeIfNecessary(value);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy