com.wms.studio.cache.lock.SyncLockMapCache.java Source code

Java tutorial

Introduction

Here is the source code for com.wms.studio.cache.lock.SyncLockMapCache.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.lock;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.rubyeye.xmemcached.MemcachedClient;

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;

import com.wms.studio.lock.LocalZookeeperLock;

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

    private static final Logger log = Logger.getLogger(SyncLockMapCache.class);
    private final String name;// MemCachekey
    private final MemcachedClient memcachedClient;
    private final LocalZookeeperLock lock;

    public SyncLockMapCache(String name, MemcachedClient memcachedClient, LocalZookeeperLock lock)
            throws CacheException {
        if (StringUtils.isBlank(name))
            throw new CacheException("key??.");
        this.name = name;
        this.memcachedClient = memcachedClient;
        this.lock = lock;
    }

    // ?????
    @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(K key, V value) throws CacheException {

        // ??????
        try {
            lock.lock();
            HashMap<K, V> attributes = memcachedClient.get(name);
            boolean isInit = false;
            if (attributes == null) {
                isInit = true;
                attributes = new HashMap<K, V>();
            }
            attributes.put(key, value);
            if (isInit) {
                memcachedClient.set(name, 0, attributes);
            } else {
                memcachedClient.replace(name, 0, attributes);
            }
            return value;
        } catch (Exception e) {
            log.fatal("MemCache,.", e);
        } finally {
            lock.unlock();
        }
        return null;
    }

    @Override
    public V remove(K key) throws CacheException {

        // ??????
        try {
            lock.lock();
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes == null) {
                return null;
            }
            V value = attributes.remove(key);
            memcachedClient.replace(name, 0, attributes);
            return value;
        } catch (Exception e) {
            log.fatal("MemCache,.", e);
        } finally {
            lock.unlock();
        }
        return null;
    }

    @Override
    public void clear() throws CacheException {
        // ??????
        try {
            lock.lock();
            memcachedClient.delete(name);
        } catch (Exception e) {
            log.fatal("MemCache,.", e);
        } finally {
            lock.unlock();
        }
    }

    @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();
    }

    public String toString() {
        return (new StringBuilder("SyncLockMapCache '")).append(name).append("'").toString();
    }

    /**
     * ?
     * 
     * @param cacheValues
     */
    public void puts(Map<K, V> cacheValues) {
        // ??????
        try {
            lock.lock();
            HashMap<K, V> attributes = memcachedClient.get(name);
            boolean isInit = false;
            if (attributes == null) {
                isInit = true;
                attributes = new HashMap<K, V>();
            }
            attributes.putAll(cacheValues);
            if (isInit) {
                memcachedClient.set(name, 0, attributes);
            } else {
                memcachedClient.replace(name, 0, attributes);
            }
        } catch (Exception e) {
            log.fatal("MemCache,.", e);
        } finally {
            lock.unlock();
        }
    }

    /**
     * ?
     * 
     * @param cacheKey
     */
    public void removes(List<K> cacheKey) {
        // ??????
        try {
            lock.lock();
            HashMap<K, V> attributes = memcachedClient.get(name);
            if (attributes == null) {
                return;
            }
            for (K key : cacheKey) {
                attributes.remove(key);
            }
            memcachedClient.replace(name, 0, attributes);
        } catch (Exception e) {
            log.fatal("MemCache,.", e);
        } finally {
            lock.unlock();
        }
    }
}