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

com.maxifier.mxcache.mbean.CacheControl Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
 */
package com.maxifier.mxcache.mbean;

import com.maxifier.mxcache.CacheFactory;
import com.maxifier.mxcache.caches.Cache;
import com.maxifier.mxcache.context.CacheContext;
import com.maxifier.mxcache.impl.resource.MxResourceFactory;
import com.maxifier.mxcache.interfaces.Statistics;
import com.maxifier.mxcache.provider.CacheDescriptor;
import com.maxifier.mxcache.provider.CacheManager;
import com.maxifier.mxcache.provider.CacheProvider;
import com.maxifier.mxcache.resource.MxResource;
import com.maxifier.mxcache.util.TIdentityHashSet;
import gnu.trove.map.hash.THashMap;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Alexander Kochurov ([email protected])
 */
public class CacheControl implements CacheControlMXBean {
    private final CacheProvider provider;

    public CacheControl(@Nonnull CacheProvider provider) {
        this.provider = provider;
    }

    @Override
    public String getCacheProviderImpl() {
        return getDisplayName(provider.getClass());
    }

    @Override
    public List getResources() {
        Set resources = MxResourceFactory.getAllResources();
        List res = new ArrayList(resources.size());
        for (MxResource resource : resources) {
            res.add(new ResourceInfo(resource.getName()));
        }
        return res;
    }

    @Override
    public List getCaches() {
        List caches = provider.getCaches();
        List res = new ArrayList(caches.size());
        for (CacheManager cacheManager : caches) {
            String impl;
            try {
                impl = cacheManager.getImplementationDetails();
            } catch (RuntimeException e) {
                impl = "Error: " + e.getMessage();
            }
            int count = 0;
            int total = 0;
            int totalHits = 0;
            int totalMisses = 0;
            double averageCalculation = 0.0;
            Set statisticsSet = new TIdentityHashSet();
            try {
                for (Cache c : CacheFactory.getCaches(cacheManager.getDescriptor())) {
                    count++;
                    total += c.getSize();
                    Statistics stat = c.getStatistics();
                    if (stat != null) {
                        statisticsSet.add(stat);
                    }
                }
            } catch (Exception e) {
                // ignore it, if a cacheManager fails to get it's data, it's not our problem
            }
            for (Statistics statistics : statisticsSet) {
                totalHits += statistics.getHits();
                totalMisses += statistics.getMisses();
                averageCalculation  += statistics.getTotalCalculationTime();
            }
            CacheDescriptor descriptor = cacheManager.getDescriptor();
            Class ownerClass = cacheManager.getOwnerClass();
            CacheContext context = cacheManager.getContext();
            res.add(new CacheInfo(context == null ? "" : context.toString(), descriptor.getKeyType() == null ? null : getDisplayName(descriptor.getKeyType()),
                                  getDisplayName(descriptor.getValueType()),
                                  descriptor.getMethod().toGenericString(),
                                  descriptor.getCacheName(), descriptor.getId(), count, total,
                                  descriptor.getGroup(),
                                  descriptor.getTags(),
                                  impl,
                                  getDisplayName(ownerClass),
                    getDisplayName(descriptor.getDeclaringClass()),
                    totalHits, totalMisses, totalMisses == 0 ? 0.0 : averageCalculation / totalMisses));
        }
        return res;
    }

    @Override
    public Map> getCachesByGroup() {
        Map> res = new THashMap>();
        for (CacheInfo cacheInfo : getCaches()) {
            String group = cacheInfo.getGroup();
            List list = res.get(group);
            if (list == null) {
                list = new ArrayList();
                res.put(group, list);
            }
            list.add(cacheInfo);
        }
        return res;
    }

    @Override
    public Map> getCachesByClass() {
        Map> res = new THashMap>();
        for (CacheInfo cacheInfo : getCaches()) {
            String owner = cacheInfo.getOwner();
            List list = res.get(owner);
            if (list == null) {
                list = new ArrayList();
                res.put(owner, list);
            }
            list.add(cacheInfo);
        }
        return res;
    }

    @Override
    public Map> getCachesByTag() {
        Map> res = new THashMap>();
        for (CacheInfo cacheInfo : getCaches()) {
            String[] tags = cacheInfo.getTags();
            if (tags != null) {
                for (String tag : tags) {
                    List list = res.get(tag);
                    if (list == null) {
                        list = new ArrayList();
                        res.put(tag, list);
                    }
                    list.add(cacheInfo);
                }
            }
        }
        return res;
    }

    @Override
    public void clearByGroup(String group) {
        CacheFactory.getCleaner().clearCacheByGroup(group);
    }

    @Override
    public void clearByTag(String tag) {
        CacheFactory.getCleaner().clearCacheByGroup(tag);
    }

    @Override
    public void clearByClass(String className) throws ClassNotFoundException {
        CacheFactory.getCleaner().clearCacheByClass(Class.forName(className));
    }

    @Override
    public void clearByResource(String resourceName) {
        MxResourceFactory.getResource(resourceName).clearDependentCaches();
    }

    private static String getDisplayName(Class ownerClass) {
        String canonicalName = ownerClass.getCanonicalName();
        return canonicalName == null ? ownerClass.getName() : canonicalName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy