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

org.springframework.data.redis.cache.plus.DelegateRedisCache 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.plus;

import java.io.Serializable;
import java.util.Date;
import java.util.concurrent.Callable;

import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheAdapter;
import org.springframework.data.redis.cache.RedisCacheElement;
import org.springframework.data.redis.cache.RedisCacheKey;

import lombok.NonNull;

/**
 * Delegate {@link RedisCache}
 */
public class DelegateRedisCache extends RedisCacheAdapter {
	
	/**
	 * Delegate {@link Cache}
	 */
	private Cache delegate;
	
	@Override
	public Cache getNativeCache() {
		
		return this.delegate;
	}
	
	/**
	 * {@link org.springframework.data.redis.cache.RedisCacheAdapter.CacheValueAccessorAdapter}
	 */
	private CacheValueAccessorAdapter accessor;
	
	/**
	 * Constructor
	 * 
	 * @param source source {@link RedisCache}
	 * @param delegate delegate {@link Cache}
	 */
	public DelegateRedisCache(RedisCache source, @NonNull Cache delegate) {
		
		super(source);
		
		this.delegate = delegate;
		this.accessor = getCacheValueAccessor(this);
	}
	
	@Override
	public  T get(Object key, Callable valueLoader) {
		
		@SuppressWarnings("unchecked")
		T value = (T) this.get(key, Object.class);
		
		return value;
	}
	
	@Override
	public RedisCacheElement get(RedisCacheKey cacheKey) {
		
		ValueWrapper wrapper = this.delegate.get(this.convertKey(cacheKey));
		
		if (wrapper == null) {
			
			return null;
		}
		
		RedisValue value = (RedisValue) wrapper.get();
		
		if (value.isExpired()) {
			
			return null;
		}
		
		return new RedisCacheElement(cacheKey, value.get());
	}
	
	@Override
	public void put(@NonNull RedisCacheElement element) {
		
		// check serialization
		this.accessor.convertToBytesIfNecessary(element.get());
		
		this.delegate.put(this.convertKey(element.getKey()), new RedisValue(element));
	}
	
	@Override
	public ValueWrapper putIfAbsent(@NonNull RedisCacheElement element) {
		
		// check serialization
		this.accessor.convertToBytesIfNecessary(element.get());
		
		return this.delegate.putIfAbsent(this.convertKey(element.getKey()), new RedisValue(element));
	}
	
	@Override
	public void evict(@NonNull RedisCacheElement element) {
		
		this.delegate.evict(this.convertKey(element.getKey()));
	}
	
	@Override
	public void clear() {
		
		this.delegate.clear();
	}
	
	/**
	 * Convert key
	 * 
	 * @param key {@link RedisCacheKey}
	 * @return converted key
	 */
	protected Object convertKey(@NonNull RedisCacheKey key) {
		
		return new String(key.getKeyBytes());
	}
	
	/**
	 * Redis value
	 */
	@SuppressWarnings("serial")
	protected static class RedisValue extends SimpleValueWrapper implements Serializable {
		
		/**
		 * Date
		 */
		private Date date;
		
		/**
		 * Value is expired?
		 * 
		 * @return {@code true} if value is expired
		 */
		public boolean isExpired() {
			
			return this.date != null && this.date.compareTo(new Date()) < 0;
		}
		
		/**
		 * Constructor
		 * 
		 * @param element {@link RedisCacheElement}
		 */
		public RedisValue(RedisCacheElement element) {
			
			super(element.get());
			
			if (!element.isEternal()) {
				
				this.date = new Date(System.currentTimeMillis() + element.getTimeToLive() * 1000);
			}
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy