
com.semanticcms.core.servlet.ConcurrentCache Maven / Gradle / Ivy
/*
* semanticcms-core-servlet - Java API for modeling web page content and relationships in a Servlet environment.
* Copyright (C) 2016 AO Industries, Inc.
* [email protected]
* 7262 Bull Pen Cir
* Mobile, AL 36695
*
* This file is part of semanticcms-core-servlet.
*
* semanticcms-core-servlet is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* semanticcms-core-servlet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with semanticcms-core-servlet. If not, see .
*/
package com.semanticcms.core.servlet;
import com.semanticcms.core.model.Page;
import com.semanticcms.core.model.PageRef;
import static com.semanticcms.core.servlet.Cache.VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.servlet.ServletException;
/**
* A page cache that is thread safe through concurrent collections.
*
* Is currently still synchronized on parent-child verifications, which only occurs on put.
*/
class ConcurrentCache extends MapCache {
private final ConcurrentMap concurrentAttributes;
ConcurrentCache() {
super(
new ConcurrentHashMap(),
VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
VERIFY_CACHE_PARENT_CHILD_RELATIONSHIPS ? new HashMap>() : null,
new ConcurrentHashMap()
);
concurrentAttributes = (ConcurrentMap)attributes;
}
/**
* Overridden to add synchronization.
*/
@Override
synchronized protected void verifyAdded(Page page) throws ServletException {
super.verifyAdded(page);
}
@Override
public ConcurrentMap newMap() {
return new ConcurrentHashMap();
}
@Override
public ConcurrentMap newMap(int size) {
return new ConcurrentHashMap(size);
}
@Override
public V getAttribute(
String key,
Class clazz,
Callable extends V,E> callable
) throws E {
V attribute = getAttribute(key, clazz);
if(attribute == null) {
attribute = callable.call();
Object existing = concurrentAttributes.putIfAbsent(key, attribute);
if(existing != null) attribute = clazz.cast(existing);
}
return attribute;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy