Skip to content

Commit 6bcd7fe

Browse files
committed
b3log#12319 缓存统计数据
1 parent 51c850f commit 6bcd7fe

File tree

2 files changed

+121
-3
lines changed

2 files changed

+121
-3
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

src/main/java/org/b3log/solo/repository/impl/StatisticRepositoryImpl.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
*/
1616
package org.b3log.solo.repository.impl;
1717

18-
18+
import org.b3log.latke.Keys;
19+
import org.b3log.latke.ioc.inject.Inject;
1920
import org.b3log.latke.repository.AbstractRepository;
21+
import org.b3log.latke.repository.RepositoryException;
2022
import org.b3log.latke.repository.annotation.Repository;
23+
import org.b3log.solo.cache.StatisticCache;
2124
import org.b3log.solo.model.Statistic;
2225
import org.b3log.solo.repository.StatisticRepository;
23-
26+
import org.json.JSONObject;
2427

2528
/**
2629
* Statistic repository.
2730
*
2831
* @author <a href="http://88250.b3log.org">Liang Ding</a>
29-
* @version 1.0.0.2, May 15, 2013
32+
* @version 1.1.0.0, Aug 30, 2017
3033
* @since 0.3.1
3134
*/
3235
@Repository
@@ -38,4 +41,42 @@ public class StatisticRepositoryImpl extends AbstractRepository implements Stati
3841
public StatisticRepositoryImpl() {
3942
super(Statistic.STATISTIC);
4043
}
44+
45+
/**
46+
* Statistic cache.
47+
*/
48+
@Inject
49+
private StatisticCache statisticCache;
50+
51+
@Override
52+
public void remove(final String id) throws RepositoryException {
53+
super.remove(id);
54+
55+
statisticCache.removeStatistic(id);
56+
}
57+
58+
@Override
59+
public JSONObject get(final String id) throws RepositoryException {
60+
JSONObject ret = statisticCache.getStatistic(id);
61+
if (null != ret) {
62+
return ret;
63+
}
64+
65+
ret = super.get(id);
66+
if (null == ret) {
67+
return null;
68+
}
69+
70+
statisticCache.putStatistic(ret);
71+
72+
return ret;
73+
}
74+
75+
@Override
76+
public void update(final String id, final JSONObject statistic) throws RepositoryException {
77+
super.update(id, statistic);
78+
79+
statistic.put(Keys.OBJECT_ID, id);
80+
statisticCache.putStatistic(statistic);
81+
}
4182
}

0 commit comments

Comments
 (0)