<%@ page contentType="text/html;charset=GBK" %>
<%
/**
 *	$RCSfile: editCache.jsp,v $
 *	$Revision: 1.1.1.1 $
 *	$Date: 2002/09/09 13:50:10 $
 */
%>

<%@ page import="java.util.*,
				 java.text.*,
				 com.jivesoftware.util.*,
                 com.jivesoftware.forum.*,
				 com.jivesoftware.forum.database.*,
				 com.jivesoftware.forum.util.*"
%>

<%!	// global variables

	// decimal formatter for cache values
	static final DecimalFormat kFormat = new DecimalFormat("#");
	static final DecimalFormat mbFormat = new DecimalFormat("#0.00");
	static final DecimalFormat percentFormat = new DecimalFormat("#0.0");

	// cache identifiers, names
	static final int[] CACHE_TYPES = {
		DatabaseCacheManager.FORUM_CACHE,
		DatabaseCacheManager.THREAD_CACHE,
		DatabaseCacheManager.MESSAGE_CACHE,
		DatabaseCacheManager.USER_CACHE,
		DatabaseCacheManager.USER_PERMS_CACHE,
		DatabaseCacheManager.GROUP_CACHE
	};
	static final String[] CACHE_NAMES = {
		"论坛",
		"主题",
		"消息",
		"用户",
		"用户权限",
		"用户组"
	};

    static final int CACHE_PRESET_SMALL = 0;
    static final int CACHE_PRESET_MEDIUM = 1;
    static final int CACHE_PRESET_LARGE = 2;
    static final int CACHE_PRESET_CUSTOM = 3;

	static final String[] CACHE_PRESET_NAMES = {
		"小",
		"中",
		"大",
		"自定义"
	};

    // Preset mem sizes (in bytes)
    static final int[][] CACHE_PRESET_SIZES = {
        {  512*1024, 1024*1024,  5120*1024 },
        {  512*1024, 3072*1024, 10240*1024 },
        { 1152*1024, 8192*1024, 32768*1024 },
        {  512*1024, 1024*1024,  6144*1024 },
        {  256*1024,  768*1024,  4608*1024 },
        {  128*1024,  256*1024,   512*1024 }
    };

	static final String[] CACHE_PRESET_DESCRIPTIONS = {
		"适合工作组或者小到中等规模的社区。(缓存区总量3 MB)",
		"适合中到大规模社区。(总量14 MB)",
		"适合非常大的用户，需要大量内存。(总量60 MB)",
		"使用此选项定义以下各项的缓存大小。"
	};

    private int getCurrentPreset(DatabaseCacheManager dbCacheManager) {
        // Custom by default (we loop through the 3 presets, if none match,
        // the current set of presets must be custom).
        int currentPreset = CACHE_PRESET_CUSTOM;
        // The array of presets to check
        int[] presetChecks = {
            CACHE_PRESET_SMALL, CACHE_PRESET_MEDIUM, CACHE_PRESET_LARGE
        };
        int preset;
        for (preset=0; preset<presetChecks.length; preset++) {
            boolean matches = true;
            for (int i=0; i<CACHE_PRESET_SIZES.length; i++) {
                if (dbCacheManager.getMaxSize(CACHE_TYPES[i]) != CACHE_PRESET_SIZES[i][preset]) {
                    matches = false;
                }
            }
            if (matches) {
                currentPreset = preset;
                break;
            }
        }
        return currentPreset;
    }
%>

<%@ include file="global.jsp" %>

<%	// Load up the DbCacheManager first. Starty by getting the underlying
    // DbForumFactory
	DbForumFactory dbForumFactory = null;
	try {
		dbForumFactory = (DbForumFactory)(((ForumFactoryProxy)forumFactory).getProxiedForumFactory());
	} catch (Exception e) { }

	// Get the cache manager
	DatabaseCacheManager cacheManager = dbForumFactory.getCacheManager();

    // get parameters
	boolean setCacheSize = ParamUtils.getBooleanParameter(request,"setCacheSize");
    boolean custom = ParamUtils.getBooleanParameter(request,"custom");
    int cachePreset = ParamUtils.getIntParameter(request,"cachePreset",getCurrentPreset(cacheManager));
    // set the cache size if requested
    if (setCacheSize) {
        if (cachePreset == CACHE_PRESET_CUSTOM) {
            custom = true;
            session.setAttribute("cache.customMode","true");
        }
        else {
            session.removeAttribute("cache.customMode");
            for (int i=0; i<CACHE_TYPES.length; i++) {
                cacheManager.setMaxSize(CACHE_TYPES[i], CACHE_PRESET_SIZES[i][cachePreset]);
            }
            response.sendRedirect("editCache.jsp?cachePreset=" + cachePreset);
            return;
        }
    }
    if ("true".equals((String)session.getAttribute("cache.customMode"))) {
        cachePreset = CACHE_PRESET_CUSTOM;
    }
    if (cachePreset == CACHE_PRESET_CUSTOM) {
        custom = true;
    }
    boolean setCustom = ParamUtils.getBooleanParameter(request,"setCustom");
    // custom cache values (in K)
    int customForumSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.FORUM_CACHE+"K",-1);
    int customThreadSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.THREAD_CACHE+"K",-1);
    int customMessageSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.MESSAGE_CACHE+"K",-1);
    int customUserSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.USER_CACHE+"K",-1);
    int customUserPermSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.USER_PERMS_CACHE+"K",-1);
    int customGroupSize = ParamUtils.getIntParameter(request,"custom"+DatabaseCacheManager.GROUP_CACHE+"K",-1);

    // Check for the "cutom" value in the session

    // set the custom sizes
    if (custom && setCustom) {
        if (customForumSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.FORUM_CACHE, customForumSize*1024);
        }
        if (customThreadSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.THREAD_CACHE, customThreadSize*1024);
        }
        if (customMessageSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.MESSAGE_CACHE, customMessageSize*1024);
        }
        if (customUserSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.USER_CACHE, customUserSize*1024);
        }
        if (customUserPermSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.USER_PERMS_CACHE, customUserPermSize*1024);
        }
        if (customGroupSize > -1) {
            cacheManager.setMaxSize(DatabaseCacheManager.GROUP_CACHE, customGroupSize*1024);
        }
        response.sendRedirect("editCache.jsp");
        return;
    }
%>

<%@ include file="header.jsp" %>

<p>

<%  // Title of this page and breadcrumbs
    String title = "编辑缓存设置";
    String[][] breadcrumbs = {
        {"主页面", "main.jsp"},
        {"缓存设置", "cache.jsp"},
        {title, "editCache.jsp"}

    };
%>
<%@ include file="title.jsp" %>

<font size="-1">
使用下面的表单自定义你的缓存设置。你不应该将缓存大小设置得大于JVM内存总量的一半。缺省情况下，大多JVM配置为使用64 MB内存（虽然缺省值可能变化或者在不同系统下值不同）。
<p>
设置太大的缓存不会提高性能。相反，调整你的缓存到有更高的效力(就像 <a href="cache.jsp">缓存设置</a> 页面测量的那样)。
</font>

<p>

<font size="-1"><b>缓存预设值</b></font>
<form action="editCache.jsp">
<input type="hidden" name="setCacheSize" value="true">
<ul>
<%  for (int i=0; i<CACHE_PRESET_NAMES.length; i++) {
        String checked = "";
        if (cachePreset == i) {
            checked = " checked";
        }
%>
<input type="radio" name="cachePreset" value="<%= i %>" id="rb0<%= i %>"<%= checked %>>
<font size="-1"><label for="rb0<%= i %>"><b><%= CACHE_PRESET_NAMES[i] %></b> -- <%= CACHE_PRESET_DESCRIPTIONS[i] %></label></font>
<p>
<%  } %>
<input type="submit" value="保存设置">
</ul>
</form>

<p>

<font size="-1"><b>缓存大小</b></font>
<form action="editCache.jsp" name="cacheForm">
<input type="hidden" name="custom" value="<%= custom %>">
<input type="hidden" name="setCustom" value="true">
<ul>
<table bgcolor="<%= tblBorderColor %>" cellpadding="0" cellspacing="0" border="0" width="300">
<tr><td>
<table bgcolor="<%= tblBorderColor %>" cellpadding="3" cellspacing="1" border="0" width="100%">
<tr bgcolor="#eeeeee">
    <td align="center" width="50%"><font face="verdana" size="-2"><b>缓存名称</b></font></td>
    <td align="center" width="50%" colspan="2"><font face="verdana" size="-2"><b>大小</b></font></td>
</tr>
<%  for (int i=0; i<CACHE_TYPES.length; i++) {
        int type = CACHE_TYPES[i];
        double sizeMB = (double)cacheManager.getMaxSize(type)/(1024*1024);
        double sizeK = (double)cacheManager.getMaxSize(type)/(1024);
%>
<tr bgcolor="#ffffff">
    <td width="50%">
    <font size="-1"><b><%= CACHE_NAMES[i] %></b></font>
    </td>
    <td width="25%">
    <%  if (custom) { %>
    <input type="text" name="custom<%= CACHE_TYPES[i] %>K" size="6" value="<%= kFormat.format(sizeK) %>" maxlength="6"><font size="-1">K</font>
    <%  } else { %>
    <font size="-1"><%= kFormat.format(sizeK) %> K</font>
    <%  } %>
    </td>
    <td width="25%">
    <font size="-1">(<%= mbFormat.format(sizeMB) %> MB)</font>
    </td>
</tr>
<%  } %>
</table>
</td></tr>
</table>
<%  if (custom) { %>
    <p>
    <input type="submit" value="保存自定义设置">
<%  } %>
<p>
<font size="-1"><i>注意: 1 MB = 1024 K</i></font>
</ul>
</form>

<p>

<center>
<form action="cache.jsp">
<input type="submit" value="返回缓存设置">
</form>
</center>

<p>

<%@ include file="footer.jsp" %>

