com.wms.studio.cache.memcache.MemCacheMapCache.java Source code

Java tutorial

Introduction

Here is the source code for com.wms.studio.cache.memcache.MemCacheMapCache.java

Source

/*
 * 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.wms.studio.cache.memcache;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.CASOperation;
import net.rubyeye.xmemcached.GetsResponse;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.util.CollectionUtils;

/**
 * @author WMS
 * 
 */
@SuppressWarnings("serial")
public class MemCacheMapCache<K, V> implements Cache<K, V>, Serializable {

    private static final Logger log = Logger.getLogger(MemCacheMapCache.class);
    private static final int reCount = 5;
    private final String name;// MemCachekey
    private final MemcachedClient memcachedClient;

    public MemCacheMapCache(String name, MemcachedClient memcachedClient) throws CacheException {
        if (StringUtils.isBlank(name))
            throw new CacheException("key??.");
        if (memcachedClient == null) {
            throw new CacheException("memcachedClient??.");
        }

        this.memcachedClient = memcachedClient;
        this.name = name;
    }

    @Override
    public V get(K key) throws CacheException {
        try {
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes != null) {
                return attributes.get(key);
            }
        } catch (Exception e) {
            log.fatal("?MemCache,.", e);
        }
        return null;
    }

    @Override
    public V put(final K key, final V value) throws CacheException {

        // ??????memcache?????
        synchronized (name) {
            try {
                // MemCachebug
                GetsResponse<V> response = memcachedClient.gets(name);

                if (response == null) {
                    memcachedClient.add(name, 0, new HashMap<>());
                }

                memcachedClient.cas(name, new CASOperation<HashMap<K, V>>() {

                    @Override
                    public int getMaxTries() {
                        return reCount;
                    }

                    @Override
                    public HashMap<K, V> getNewValue(long currentCAS, HashMap<K, V> currentValue) {

                        if (currentValue == null) {
                            currentValue = new HashMap<>();
                        }
                        currentValue.put(key, value);
                        return currentValue;
                    }
                });
            } catch (TimeoutException | InterruptedException | MemcachedException e) {
                log.fatal("MemCache", e);
                return null;
            }
        }

        return value;
    }

    @Override
    public V remove(final K key) throws CacheException {
        // ??????memcache?????
        synchronized (name) {
            try {
                memcachedClient.cas(name, new CASOperation<HashMap<K, V>>() {

                    @Override
                    public int getMaxTries() {
                        return reCount;
                    }

                    @Override
                    public HashMap<K, V> getNewValue(long currentCAS, HashMap<K, V> currentValue) {

                        if (currentValue == null) {
                            currentValue = new HashMap<>();
                        }
                        currentValue.remove(key);
                        return currentValue;
                    }
                });
            } catch (TimeoutException | InterruptedException | MemcachedException e) {
                log.fatal("MemCache", e);
            }
        }

        return null;
    }

    @Override
    public void clear() throws CacheException {
        // ??????memcache?????
        synchronized (name) {
            try {
                memcachedClient.cas(name, new CASOperation<HashMap<K, V>>() {

                    @Override
                    public int getMaxTries() {
                        return reCount;
                    }

                    @Override
                    public HashMap<K, V> getNewValue(long currentCAS, HashMap<K, V> currentValue) {

                        if (currentValue == null) {
                            currentValue = new HashMap<>();
                        }
                        currentValue.clear();
                        return currentValue;
                    }
                });
            } catch (TimeoutException | InterruptedException | MemcachedException e) {
                log.fatal("MemCache", e);
            }
        }
    }

    @Override
    public int size() {
        try {
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes != null) {
                return attributes.size();
            }
        } catch (Exception e) {
            log.fatal("?MemCache,.", e);
        }
        return 0;
    }

    @Override
    public Set<K> keys() {
        try {
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes != null) {
                Set<K> keys = attributes.keySet();
                if (!keys.isEmpty())
                    return Collections.unmodifiableSet(keys);
            }
        } catch (Exception e) {
            log.fatal("?MemCache,.", e);
        }
        return Collections.emptySet();
    }

    @Override
    public Collection<V> values() {
        try {
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes != null) {
                Collection<V> values = attributes.values();
                if (!CollectionUtils.isEmpty(values))
                    return Collections.unmodifiableCollection(values);
            }
        } catch (Exception e) {
            log.fatal("?MemCache,.", e);
        }
        return Collections.emptySet();
    }

}