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

com.turbospaces.ebean.LocalEbeanCache Maven / Gradle / Ivy

There is a newer version: 2.0.33
Show newest version
package com.turbospaces.ebean;

import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

import org.apache.commons.lang3.exception.ExceptionUtils;

import com.google.common.collect.ImmutableSet;
import com.turbospaces.cache.BlockhoundCacheWrapper;
import com.turbospaces.cfg.ApplicationProperties;

import io.ebean.cache.ServerCacheConfig;
import io.ebean.config.CurrentTenantProvider;

public class LocalEbeanCache extends AbstractEbeanCache {
    private final BroadcastChannel dispatcher;

    public LocalEbeanCache(
            ApplicationProperties props,
            String cacheKey,
            BlockhoundCacheWrapper cache,
            CurrentTenantProvider tenantProvider,
            ServerCacheConfig config,
            BroadcastChannel dispatcher) {
        super(props, cacheKey, cache, tenantProvider, config);
        this.dispatcher = Objects.requireNonNull(dispatcher);
    }
    @Override
    public void put(Object id, Object obj) {
        String key = key(id);
        super.put(id, obj);
        try {
            dispatcher.broadcastRemoveAsync(cacheKey(), key);
        } catch (Throwable err) {
            ExceptionUtils.wrapAndThrow(err);
        }
    }
    @Override
    public void putAll(Map keyValues) {
        ImmutableSet.Builder transformed = ImmutableSet.builder();
        for (Entry entry : keyValues.entrySet()) {
            transformed.add(key(entry.getKey()));
            super.put(entry.getKey(), entry.getValue());
        }
        try {
            dispatcher.broadcastRemoveAllAsync(cacheKey(), transformed.build());
        } catch (Throwable err) {
            ExceptionUtils.wrapAndThrow(err);
        }
    }
    @Override
    public void remove(Object id) {
        String key = key(id);
        super.remove(id);
        try {
            dispatcher.broadcastRemoveAsync(cacheKey(), key);
        } catch (Throwable err) {
            ExceptionUtils.wrapAndThrow(err);
        }
    }
    @Override
    public void removeAll(Set keys) {
        ImmutableSet.Builder transformed = ImmutableSet.builder();
        for (Object it : keys) {
            transformed.add(key(it));
            super.remove(it);
        }
        try {
            dispatcher.broadcastRemoveAllAsync(cacheKey(), transformed.build());
        } catch (Throwable err) {
            ExceptionUtils.wrapAndThrow(err);
        }
    }
    @Override
    public void clear() {
        super.clear();
        dispatcher.broadcastClearAllAsync();
    }
}