com.dingtalk.spring.boot.cache.LocalCacheImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2018, hiwepy (https://github.com/hiwepy).
*
* 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 com.dingtalk.spring.boot.cache;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
public class LocalCacheImpl implements LocalCache {
private static final long MAX_SIZE = 65535;
private static final long EXPIRE_TIME = 10;
private Cache> caches = CacheBuilder.newBuilder().maximumSize(MAX_SIZE)
.expireAfterAccess(EXPIRE_TIME, TimeUnit.SECONDS).removalListener(new RemovalListener>() {
@Override
public void onRemoval(RemovalNotification> notification) {
// TODO
}
}).build();
@Override
public V get(K key) throws Exception {
Optional opt = caches.get(key, new Callable>() {
@Override
public Optional call() throws Exception {
// TODO获取数据,加入缓存
return Optional.fromNullable(null);
}
});
return opt.isPresent() ? opt.get() : null;
}
@Override
public void put(K key, V value) {
caches.put(key, Optional.of(value));
}
@Override
public void remove(Object key) {
caches.invalidate(key);
}
}