messagegovernor.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 79 行

JAVA
79
字号
/** * $RCSfile: MessageGovernor.java,v $ * $Revision: 1.2 $ * $Date: 2002/07/16 00:11:06 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.util;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Provides a mechanism to limit the rate at which a user may post messages. The Jive property * <tt>postRateInterval</tt> is the number of seconds that must elapse before a user is allowed * to post a message again. The default value is 30 seconds.<p> * * @author Matt Tucker */public class MessageGovernor {    private static long postInterval = 30*JiveGlobals.SECOND;    static {        String interval = JiveGlobals.getJiveProperty("postRateInterval");        if (interval != null) {            try {                postInterval = Long.parseLong(interval) * JiveGlobals.SECOND;            }            catch (NumberFormatException nfe) {                nfe.printStackTrace();            }        }    }    private static Cache postLimitCache = CacheFactory.createCache("postLimitCache", 2*1024,            postInterval);    /**     * Returns true if the user should be allowed to post a new message and false otherwise. A user     * is allowed to post a new message if the post interval has elapsed since the last post.     * If false is returned, the user should be presented with a message to try their post again     * after a short wait.     *     * @param user the wishes to create a new message.     * @return true if the post should be allowed and false if it shouldn't.     */    public static boolean attemptNewMessage(User user) {        Long userID = new Long(user.getID());        // Check the cache to see if it contains an entry for the specified user. If it does,        // then we know that the user shouldn't be allowed to make a post since the post        // interval hasn't elapsed.        if (postLimitCache.get(userID) != null) {            return false;        }        // Otherwise, the user is allowed to make a post. Add an entry to the cache that will        // expire after the post interval elapses so        else {            postLimitCache.put(userID, userID);            return true;        }    }    /**     * Returns the length of time in seconds a user must wait before     * posting a new message.     *     * @return the length of time in seconds a user must wait before posting     * a new message.     */    public static int getPostInterval() {        return (int)(postInterval/1000);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?