Skip to content

Commit

Permalink
✨ 页面静态化 #107
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Apr 14, 2020
1 parent 846641e commit 9b7ba22
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void updateLink(final RequestContext context) {
final JsonRenderer renderer = new JsonRenderer();
context.setRenderer(renderer);
final JSONObject ret = new JSONObject();

try {
final JSONObject requestJSON = context.requestJSON();
linkMgmtService.updateLink(requestJSON);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.SkinMgmtService;
import org.b3log.solo.util.Skins;
import org.b3log.solo.util.Statics;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -168,6 +169,8 @@ public void updateSkin(final RequestContext context) {

ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));

Statics.clear();
} catch (final ServiceException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);

Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/b3log/solo/service/LinkMgmtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.model.Link;
import org.b3log.solo.repository.LinkRepository;
import org.b3log.solo.util.Statics;
import org.json.JSONObject;

/**
* Link management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.2, Oct 23, 2019
* @version 1.0.0.3, Apr 15, 2020
* @since 0.4.0
*/
@Service
Expand All @@ -52,11 +53,11 @@ public class LinkMgmtService {
*/
public void removeLink(final String linkId) throws ServiceException {
final Transaction transaction = linkRepository.beginTransaction();

try {
linkRepository.remove(linkId);

transaction.commit();

Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down Expand Up @@ -88,12 +89,11 @@ public void updateLink(final JSONObject requestJSONObject) throws ServiceExcepti
final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
final String linkId = link.getString(Keys.OBJECT_ID);
final JSONObject oldLink = linkRepository.get(linkId);

link.put(Link.LINK_ORDER, oldLink.getInt(Link.LINK_ORDER));

linkRepository.update(linkId, link);

transaction.commit();

Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down Expand Up @@ -143,8 +143,9 @@ public void changeOrder(final String linkId, final String direction) throws Serv

linkRepository.update(srcLink.getString(Keys.OBJECT_ID), srcLink);
linkRepository.update(targetLink.getString(Keys.OBJECT_ID), targetLink);

transaction.commit();

Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down Expand Up @@ -173,16 +174,15 @@ public void changeOrder(final String linkId, final String direction) throws Serv
*/
public String addLink(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = linkRepository.beginTransaction();

try {
final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
final int maxOrder = linkRepository.getMaxOrder();

link.put(Link.LINK_ORDER, maxOrder + 1);
final String ret = linkRepository.add(link);

transaction.commit();

Statics.clear();

return ret;
} catch (final Exception e) {
if (transaction.isActive()) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/org/b3log/solo/service/PageMgmtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@
import org.apache.logging.log4j.Logger;
import org.b3log.latke.Keys;
import org.b3log.latke.ioc.Inject;
import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException;
import org.b3log.latke.service.annotation.Service;
import org.b3log.solo.model.Page;
import org.b3log.solo.repository.CommentRepository;
import org.b3log.solo.repository.PageRepository;
import org.json.JSONException;
import org.b3log.solo.util.Statics;
import org.json.JSONObject;

/**
* Page management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="http://vanessa.b3log.org">Vanessa</a>
* @version 1.1.0.19, Jun 6, 2019
* @version 1.1.0.20, Apr 15, 2020
* @since 0.4.0
*/
@Service
Expand Down Expand Up @@ -128,6 +127,8 @@ public void updatePage(final JSONObject requestJSONObject) throws ServiceExcepti
pageRepository.update(pageId, newPage);
transaction.commit();

Statics.clear();

LOGGER.log(Level.DEBUG, "Updated a page[id={}]", pageId);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
Expand All @@ -151,6 +152,8 @@ public void removePage(final String pageId) throws ServiceException {
pageRepository.remove(pageId);
commentRepository.removeComments(pageId);
transaction.commit();

Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down Expand Up @@ -199,15 +202,10 @@ public String addPage(final JSONObject requestJSONObject) throws ServiceExceptio
final String ret = pageRepository.add(page);
transaction.commit();

return ret;
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
Statics.clear();

throw new ServiceException(e);
} catch (final RepositoryException e) {
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
Expand Down Expand Up @@ -252,6 +250,8 @@ public void changeOrder(final String pageId, final String direction) throws Serv
pageRepository.update(srcPage.getString(Keys.OBJECT_ID), srcPage, Page.PAGE_ORDER);
pageRepository.update(targetPage.getString(Keys.OBJECT_ID), targetPage, Page.PAGE_ORDER);
transaction.commit();

Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.b3log.solo.model.Option;
import org.b3log.solo.repository.OptionRepository;
import org.b3log.solo.util.Markdowns;
import org.b3log.solo.util.Statics;
import org.json.JSONObject;

import java.util.Iterator;
Expand All @@ -34,7 +35,7 @@
* Preference management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.4.0.6, Apr 6, 2020
* @version 1.4.0.7, Apr 15, 2020
* @since 0.4.0
*/
@Service
Expand Down Expand Up @@ -79,7 +80,6 @@ public void updatePreference(final JSONObject preference) throws ServiceExceptio
}

final Transaction transaction = optionRepository.beginTransaction();

try {
preference.put(Option.ID_C_SIGNS, preference.get(Option.ID_C_SIGNS).toString());

Expand Down Expand Up @@ -253,6 +253,7 @@ public void updatePreference(final JSONObject preference) throws ServiceExceptio
transaction.commit();

Markdowns.clearCache();
Statics.clear();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/b3log/solo/util/Statics.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ public static void put(final RequestContext context) {
}
}

/**
* Clears all files under ~/.solo/static-cache.
*/
public static void clear() {
try {
FileUtils.cleanDirectory(DIR);
Expand Down

0 comments on commit 9b7ba22

Please # to comment.