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

win.doyto.query.service.CachedDataAccess Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
// Generated by delombok at Wed Nov 06 09:48:14 UTC 2024
/*
 * Copyright © 2019-2024 Forb Yuan
 *
 * 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 win.doyto.query.service;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.cache.CacheManager;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import win.doyto.query.cache.CacheWrapper;
import win.doyto.query.core.DataAccess;
import win.doyto.query.core.DoytoQuery;
import win.doyto.query.core.IdWrapper;
import win.doyto.query.entity.Persistable;
import java.io.Serializable;
import java.util.List;

/**
 * CachedDataAccess
 *
 * @author f0rb on 2023/6/16
 * @since 1.0.2
 */
public class CachedDataAccess, I extends Serializable, Q extends DoytoQuery> implements DataAccess {
    protected final DataAccess delegate;
    protected final CacheWrapper entityCacheWrapper = CacheWrapper.createInstance();
    protected final CacheWrapper> queryCacheWrapper = CacheWrapper.createInstance();

    public CachedDataAccess(DataAccess dataAccess, CacheManager cacheManager, String cacheName) {
        this.delegate = dataAccess;
        entityCacheWrapper.setCache(cacheManager.getCache(cacheName));
        queryCacheWrapper.setCache(cacheManager.getCache(cacheName + ":query"));
    }

    private String resolveCacheKey(IdWrapper w) {
        return w.toCacheKey();
    }

    private void clearCache() {
        entityCacheWrapper.clear();
        queryCacheWrapper.clear();
    }

    private void evictCache(String key) {
        entityCacheWrapper.evict(key);
        queryCacheWrapper.clear();
    }

    @Override
    public int batchInsert(Iterable entities, String... columns) {
        try {
            return delegate.batchInsert(entities, columns);
        } finally {
            clearCache();
        }
    }

    @Override
    public int patch(E e, Q q) {
        try {
            return delegate.patch(e, q);
        } finally {
            clearCache();
        }
    }

    protected String generateCacheKey(Q query) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            return null;
        }
        return ToStringBuilder.reflectionToString(query, NonNullToStringStyle.NO_CLASS_NAME_NON_NULL_STYLE);
    }

    @Override
    public List query(Q query) {
        String key = generateCacheKey(query);
        return queryCacheWrapper.execute(key, () -> delegate.query(query));
    }

    @Override
    public E get(IdWrapper w) {
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            return delegate.get(w);
        }
        return entityCacheWrapper.execute(resolveCacheKey(w), () -> delegate.get(w));
    }

    @Override
    public int delete(IdWrapper w) {
        try {
            return delegate.delete(w);
        } finally {
            String key = resolveCacheKey(w);
            evictCache(key);
            entityCacheWrapper.execute(key, () -> null);
        }
    }

    @Override
    public int delete(Q query) {
        try {
            return delegate.delete(query);
        } finally {
            clearCache();
        }
    }

    @Override
    public void create(E e) {
        delegate.create(e);
        evictCache(resolveCacheKey(e.toIdWrapper()));
    }

    @Override
    public int update(E e) {
        try {
            return delegate.update(e);
        } finally {
            evictCache(resolveCacheKey(e.toIdWrapper()));
        }
    }

    @Override
    public int patch(E e) {
        try {
            return delegate.patch(e);
        } finally {
            evictCache(resolveCacheKey(e.toIdWrapper()));
        }
    }


    /**
     * NonNullToStringStyle
     *
     * @author f0rb on 2019-07-14
     */
    private static class NonNullToStringStyle extends ToStringStyle {
        public static final NonNullToStringStyle NO_CLASS_NAME_NON_NULL_STYLE = new NonNullToStringStyle(false);

        private NonNullToStringStyle(boolean useClassName) {
            this.setUseClassName(useClassName);
            this.setUseIdentityHashCode(false);
        }

        @Override
        public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
            if (value != null) {
                super.append(buffer, fieldName, value, fullDetail);
            }
        }
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public long count(final Q query) {
        return this.delegate.count(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public win.doyto.query.core.PageList page(final Q query) {
        return this.delegate.page(query);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public  java.util.List queryColumns(final Q q, final java.lang.Class clazz, final java.lang.String... columns) {
        return this.delegate.queryColumns(q, clazz, columns);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public E get(final I id) {
        return this.delegate.get(id);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public int delete(final I id) {
        return this.delegate.delete(id);
    }

    @java.lang.SuppressWarnings("all")
    @lombok.Generated
    public java.util.List queryIds(final Q query) {
        return this.delegate.queryIds(query);
    }
}