|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2017, b3log.org & hacpai.com |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.b3log.solo.cache; |
| 17 | + |
| 18 | +import org.b3log.latke.Keys; |
| 19 | +import org.b3log.latke.cache.Cache; |
| 20 | +import org.b3log.latke.cache.CacheFactory; |
| 21 | +import org.b3log.latke.ioc.inject.Named; |
| 22 | +import org.b3log.latke.ioc.inject.Singleton; |
| 23 | +import org.b3log.solo.model.Statistic; |
| 24 | +import org.b3log.solo.util.JSONs; |
| 25 | +import org.json.JSONObject; |
| 26 | + |
| 27 | +/** |
| 28 | + * Statistic cache. |
| 29 | + * |
| 30 | + * @author <a href="http://88250.b3log.org">Liang Ding</a> |
| 31 | + * @version 1.0.0.0, Aug 30, 2017 |
| 32 | + * @since 2.3.0 |
| 33 | + */ |
| 34 | +@Named |
| 35 | +@Singleton |
| 36 | +public class StatisticCache { |
| 37 | + |
| 38 | + /** |
| 39 | + * Statistic cache. |
| 40 | + */ |
| 41 | + private Cache cache = CacheFactory.getCache(Statistic.STATISTIC); |
| 42 | + |
| 43 | + /** |
| 44 | + * Gets an statistic by the specified statistic id. |
| 45 | + * |
| 46 | + * @param id the specified statistic id |
| 47 | + * @return statistic, returns {@code null} if not found |
| 48 | + */ |
| 49 | + public JSONObject getStatistic(final String id) { |
| 50 | + final JSONObject statistic = cache.get(id); |
| 51 | + if (null == statistic) { |
| 52 | + return null; |
| 53 | + } |
| 54 | + |
| 55 | + return JSONs.clone(statistic); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Adds or updates the specified statistic. |
| 60 | + * |
| 61 | + * @param statistic the specified statistic |
| 62 | + */ |
| 63 | + public void putStatistic(final JSONObject statistic) { |
| 64 | + final String statisticId = statistic.optString(Keys.OBJECT_ID); |
| 65 | + |
| 66 | + cache.put(statisticId, JSONs.clone(statistic)); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Removes an statistic by the specified statistic id. |
| 71 | + * |
| 72 | + * @param id the specified statistic id |
| 73 | + */ |
| 74 | + public void removeStatistic(final String id) { |
| 75 | + cache.remove(id); |
| 76 | + } |
| 77 | +} |
0 commit comments