org.covito.kit.cache.support.EhCache.java Source code

Java tutorial

Introduction

Here is the source code for org.covito.kit.cache.support.EhCache.java

Source

/*
 * Copyright 2010-2014  All rights reserved.
 *
 * 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 org.covito.kit.cache.support;

import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import net.sf.ehcache.Status;

import org.covito.kit.cache.common.AbsCacheImpl;
import org.covito.kit.cache.common.Node;
import org.springframework.util.Assert;

/**
 * EhCache
 * <p>
 * ??
 * </p>
 * 
 * @author covito
 * @version [v1.0, 2014610]
 */
public class EhCache<K, V> extends AbsCacheImpl<K, V> {

    private final Ehcache cache;

    /**
     * Constructor
     * 
     * @param cacheManager
     * @param cache
     */
    public EhCache(Ehcache cache) {
        Assert.notNull(cache, "Ehcache must not be null");
        Status status = cache.getStatus();
        Assert.isTrue(Status.STATUS_ALIVE.equals(status),
                "An 'alive' Ehcache is required - current cache is " + status.toString());
        this.cache = cache;
    }

    /**
     * {@inheritDoc}
     * 
     * @author covito
     * @return
     */
    @Override
    public String getName() {
        return cache.getName();
    }

    /**
     * {@inheritDoc}
     * 
     * @author covito
     * @return
     */
    @Override
    public Object getNativeCache() {
        return this.cache;
    }

    /**
     * {@inheritDoc}
     * 
     * @author covito
     */
    @Override
    public void removeAll() {
        this.cache.removeAll();
    }

    @SuppressWarnings("unchecked")
    @Override
    protected Node<K, V> getNode(K key) {
        Element ele = this.cache.get(key);
        if (ele == null) {
            return null;
        }
        return (Node<K, V>) ele.getObjectValue();
    }

    @Override
    protected void putNode(Node<K, V> n) {
        this.cache.put(new Element(n.getKey(), n));
    }

    @Override
    protected void removeNode(K key) {
        this.cache.remove(key);
    }

}