Commit bcbd8a6f authored by Liang Ding's avatar Liang Ding

#12512

parent 71cd2763
......@@ -26,11 +26,14 @@ import org.b3log.solo.model.Option;
import org.b3log.solo.util.JSONs;
import org.json.JSONObject;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Option cache.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Jul 16, 2017
* @version 1.1.0.0, Sep 19, 2018
* @since 2.3.0
*/
@Named
......@@ -40,7 +43,46 @@ public class OptionCache {
/**
* Option cache.
*/
private Cache CACHE = CacheFactory.getCache(Option.OPTIONS);
private static final Cache CACHE = CacheFactory.getCache(Option.OPTIONS);
/**
* Category option caches.
*/
private static final Map<String, JSONObject> CATEGORY_CACHES = new ConcurrentHashMap<>();
/**
* Removes a category cache specified by the given category.
*
* @param category the given category
*/
public void removeCategory(final String category) {
CATEGORY_CACHES.remove(category);
}
/**
* Gets merged options as a JSON object for the specified category
*
* @param category the specified category
* @return merged options
*/
public JSONObject getCategory(final String category) {
JSONObject ret = CATEGORY_CACHES.get(category);
if (null == ret) {
return null;
}
return JSONs.clone(ret);
}
/**
* Puts the specified merged options with the specified category.
*
* @param category the specified category
* @param mergedOptions the specified merged options
*/
public void putCategory(final String category, final JSONObject mergedOptions) {
CATEGORY_CACHES.put(category, mergedOptions);
}
/**
* Gets an option by the specified option id.
......@@ -64,6 +106,9 @@ public class OptionCache {
*/
public void putOption(final JSONObject option) {
CACHE.put(option.optString(Keys.OBJECT_ID), JSONs.clone(option));
final String category = option.optString(Option.OPTION_CATEGORY);
removeCategory(category);
}
/**
......@@ -72,6 +117,14 @@ public class OptionCache {
* @param id the specified option id
*/
public void removeOption(final String id) {
final JSONObject option = getOption(id);
if (null == option) {
return;
}
final String category = option.optString(Option.OPTION_CATEGORY);
removeCategory(category);
CACHE.remove(id);
}
}
......@@ -17,15 +17,34 @@
*/
package org.b3log.solo.repository;
import org.b3log.latke.repository.Repository;
import org.b3log.latke.repository.RepositoryException;
import org.json.JSONObject;
/**
* Option repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Jan 24, 2013
* @version 1.1.0.0, Sep 19, 2018
* @since 0.6.0
*/
public interface OptionRepository extends Repository {}
public interface OptionRepository extends Repository {
/**
* Gets options with the specified category.
* <p>
* All options with the specified category will be merged into one json object as the return value.
* </p>
*
* @param category the specified category
* @return all options with the specified category, for example,
* <pre>
* {
* "${optionId}": "${optionValue}",
* ....
* }
* </pre>, returns {@code null} if not found
* @throws RepositoryException repository exception
*/
JSONObject getOptions(final String category) throws RepositoryException;
}
......@@ -19,19 +19,20 @@ package org.b3log.solo.repository.impl;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.inject.Inject;
import org.b3log.latke.repository.AbstractRepository;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.*;
import org.b3log.latke.repository.annotation.Repository;
import org.b3log.solo.cache.OptionCache;
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.json.JSONObject;
import java.util.List;
/**
* Option repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Jul 16, 2017
* @version 1.0.0.2, Sep 19, 2018
* @since 0.6.0
*/
@Repository
......@@ -52,9 +53,16 @@ public class OptionRepositoryImpl extends AbstractRepository implements OptionRe
@Override
public void remove(final String id) throws RepositoryException {
super.remove(id);
final JSONObject option = get(id);
if (null == option) {
return;
}
super.remove(id);
optionCache.removeOption(id);
final String category = option.optString(Option.OPTION_CATEGORY);
optionCache.removeCategory(category);
}
@Override
......@@ -81,4 +89,26 @@ public class OptionRepositoryImpl extends AbstractRepository implements OptionRe
option.put(Keys.OBJECT_ID, id);
optionCache.putOption(option);
}
@Override
public JSONObject getOptions(final String category) throws RepositoryException {
final JSONObject cached = optionCache.getCategory(category);
if (null != cached) {
return cached;
}
final JSONObject ret = new JSONObject();
try {
final List<JSONObject> options = getList(new Query().setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, category)));
if (0 == options.size()) {
return null;
}
options.stream().forEach(option -> ret.put(option.optString(Keys.OBJECT_ID), option.opt(Option.OPTION_VALUE)));
optionCache.putCategory(category, ret);
return ret;
} catch (final Exception e) {
throw new RepositoryException(e);
}
}
}
......@@ -17,24 +17,18 @@
*/
package org.b3log.solo.service;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.inject.Inject;
import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.PropertyFilter;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.json.JSONArray;
import org.json.JSONObject;
/**
* Option query service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.1, Jul 22, 2017
* @version 1.0.0.2, Sep 19, 2018
* @since 0.6.0
*/
@Service
......@@ -78,25 +72,8 @@ public class OptionQueryService {
* @throws ServiceException service exception
*/
public JSONObject getOptions(final String category) throws ServiceException {
final Query query = new Query().
setFilter(new PropertyFilter(Option.OPTION_CATEGORY, FilterOperator.EQUAL, category));
try {
final JSONObject result = optionRepository.get(query);
final JSONArray options = result.getJSONArray(Keys.RESULTS);
if (0 == options.length()) {
return null;
}
final JSONObject ret = new JSONObject();
for (int i = 0; i < options.length(); i++) {
final JSONObject option = options.getJSONObject(i);
ret.put(option.getString(Keys.OBJECT_ID), option.opt(Option.OPTION_VALUE));
}
return ret;
return optionRepository.getOptions(category);
} catch (final Exception e) {
throw new ServiceException(e);
}
......
......@@ -18,10 +18,8 @@
package org.b3log.solo.service;
import org.b3log.latke.ioc.inject.Inject;
import org.b3log.latke.logging.Logger;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.cache.StatisticCache;
import org.b3log.solo.model.Option;
import org.json.JSONObject;
......@@ -29,29 +27,18 @@ import org.json.JSONObject;
* Statistic query service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.0.0.0, Sep 6, 2017
* @version 2.0.0.1, Sep 19, 2018
* @since 0.5.0
*/
@Service
public class StatisticQueryService {
/**
* Logger.
*/
private static final Logger LOGGER = Logger.getLogger(StatisticQueryService.class);
/**
* Option query service.
*/
@Inject
private OptionQueryService optionQueryService;
/**
* Statistic cache.
*/
@Inject
private StatisticCache statisticCache;
/**
* Gets the online visitor count.
*
......@@ -128,13 +115,7 @@ public class StatisticQueryService {
* @throws ServiceException if repository exception
*/
public JSONObject getStatistic() throws ServiceException {
JSONObject ret = statisticCache.getStatistic();
if (null == ret) {
ret = optionQueryService.getOptions(Option.CATEGORY_C_STATISTIC);
statisticCache.putStatistic(ret);
}
return ret;
return optionQueryService.getOptions(Option.CATEGORY_C_STATISTIC);
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment