databasecachemanager.java

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

JAVA
576 行
字号
            catch (Exception e) { }        }        cacheSize = JiveGlobals.getJiveProperty("cache.attachmentCache.size");        if (cacheSize != null) {            try {  attachmentCacheSize = Integer.parseInt(cacheSize);  }            catch (Exception e) { }        }        // Initialize all caches        categoryCache = CacheFactory.createCache("Category", categoryCacheSize, 6*HOUR);        forumCache = CacheFactory.createCache("Forum", forumCacheSize, 6*HOUR);        forumIDCache = CacheFactory.createCache("Forum ID Map",                forumIDCacheSize, 6*HOUR);        threadCache = CacheFactory.createCache("Thread", threadCacheSize, 6*HOUR);        messageCache = CacheFactory.createCache("Message", messageCacheSize, 6*HOUR);        userCache = CacheFactory.createCache("User", userCacheSize, 6*HOUR);        groupCache = CacheFactory.createCache("Group", groupCacheSize, 6*HOUR);        userIDCache = CacheFactory.createCache("User ID Map", userIDCacheSize, 6*HOUR);        userPermsCache = CacheFactory.createCache("User Permissions",                userPermCacheSize, 6*HOUR);        groupIDCache = CacheFactory.createCache("Group ID Map",                groupIDCacheSize, 6*HOUR);        watchCache = CacheFactory.createCache("Watches", watchCacheSize, 1*HOUR);        userMessageCountCache = CacheFactory.createCache("User's Message Count",                userMsgCountCacheSize, 6*HOUR);        queryCache = CacheFactory.createCache("Query",                queryCacheSize, 6*HOUR);        groupMemberCache = CacheFactory.createCache("Group Membership",                groupMemberCacheSize, 6*HOUR);        attachmentCache = CacheFactory.createCache("Attachment Cache",                attachmentCacheSize, 6*HOUR);        // Initialize the short-term query cache        initShortTermQueryCache();    }    /**     * Initializes the short-term query cache.     */    private void initShortTermQueryCache() {        String cacheTime = null;        String cacheSize = null;        int shortTermQueryCacheSize = 128*1024; // 1/8 MB        long shortTermQueryCacheTime = 0;        cacheSize = JiveGlobals.getJiveProperty("cache.shortTermQueryCache.size");        if (cacheSize != null) {            try {  shortTermQueryCacheSize = Integer.parseInt(cacheSize);  }            catch (Exception e) { }        }        cacheTime = JiveGlobals.getJiveProperty("cache.shortTermQueryCache.time");        if (cacheTime != null) {            try {  shortTermQueryCacheTime = Long.parseLong(cacheTime);  }            catch (Exception e) { }        }        // In order for the short term query cache to be enabled, the user        // must set the expiration time to be greater than 0.        if (shortTermQueryCacheTime > 0) {            shortTermQueryCacheEnabled = true;            shortTermQueryCache = CacheFactory.createCache("Short-term Query",                    shortTermQueryCacheSize, shortTermQueryCacheTime);        }    }    /**     * Persists current max cache sizes as Jive properties so that they'll be     * used the next time cache is initialized.     */    public void saveCacheSettings() {        JiveGlobals.setJiveProperty("cache.categoryCache.size",                String.valueOf(categoryCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.forumCache.size",                String.valueOf(forumCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.forumIDCache.size",                String.valueOf(forumIDCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.threadCache.size",                String.valueOf(threadCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.messageCache.size",                String.valueOf(messageCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.userCache.size",                String.valueOf(userCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.userPermsCache.size",                String.valueOf(userPermsCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.groupCache.size",                String.valueOf(groupCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.userIDCache.size",                String.valueOf(userIDCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.groupIDCache.size",                String.valueOf(groupIDCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.watchCache.size",                String.valueOf(watchCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.userMsgCountCache.size",                String.valueOf(userMessageCountCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.queryCache.size",                String.valueOf(queryCache.getMaxCacheSize()));        if (shortTermQueryCacheEnabled) {            JiveGlobals.setJiveProperty("cache.shortTermQueryCache.size",                    String.valueOf(shortTermQueryCache.getMaxCacheSize()));        }        JiveGlobals.setJiveProperty("cache.groupMemberCache.size",                String.valueOf(groupMemberCache.getMaxCacheSize()));        JiveGlobals.setJiveProperty("cache.attachmentCache.size",                String.valueOf(attachmentCache.getMaxCacheSize()));    }    /**     * Returns true if cache is globally enabled. Cache should only be disabled     * for testing purposes, since it has a huge impact on performance.     *     * @return true if cache is globally enabled.     */    public boolean isCacheEnabled() {        return cacheEnabled;    }    /**     * Sets the cache to be enabled or disabled globally. Cache should only be     * disabled for testing purposes, since it has a huge impact on performance.     *     * @param cacheEnabled true if cache should be globally enabled.     */    public void setCacheEnabled(boolean cacheEnabled) {        // If we're setting cacheEnabled to false, clear all caches        if (cacheEnabled == false) {            userCache.clear();            userIDCache.clear();            groupCache.clear();            groupIDCache.clear();            categoryCache.clear();            forumCache.clear();            forumIDCache.clear();            threadCache.clear();            messageCache.clear();            userPermsCache.clear();            queryCache.clear();            if (shortTermQueryCacheEnabled) {                shortTermQueryCache.clear();            }            watchCache.clear();            groupMemberCache.clear();            userMessageCountCache.clear();            attachmentCache.clear();        }        this.cacheEnabled = cacheEnabled;        JiveGlobals.setJiveProperty("cache.enabled", String.valueOf(cacheEnabled));    }    /**     * Returns true if the short term query cache is enabled. It is only     * enabled if the Jive property "cache.shortTermQueryCache.time" has been     * set.     *     * @return true if the short term query cache is enabled.     */    public boolean isShortTermQueryCacheEnabled() {        return shortTermQueryCacheEnabled;    }    public void setShortTermQueryCacheEnabled(boolean shortTermQueryCacheEnabled) {        // Turn it on:        if (shortTermQueryCacheEnabled) {            // To enable the Short-term Query cache the expiration time of the cache            // must be greater than 0 so we'll enable the cache to it's default            // time of 5 seconds (5000 milliseconds).            JiveGlobals.setJiveProperty("cache.shortTermQueryCache.time","5000");            initShortTermQueryCache();        }        // Turn it off:        else {            this.shortTermQueryCacheEnabled = false;            JiveGlobals.deleteJiveProperty("cache.shortTermQueryCache.time");            shortTermQueryCache.clear();        }    }    DbForumCategory getForumCategory(long categoryID) throws ForumCategoryNotFoundException {        if (!cacheEnabled) {            return new DbForumCategory(categoryID);        }        // Cache is enabled.        DbForumCategory category = (DbForumCategory)categoryCache.get(new Long(categoryID));        if (category == null) {            synchronized (Long.toString(categoryID).intern()) {                category = (DbForumCategory)categoryCache.get(new Long(categoryID));                if (category == null) {                    category = new DbForumCategory(categoryID);                    categoryCache.put(new Long(categoryID), category);                }            }        }        return category;    }    DbForum getForum(long forumID) throws ForumNotFoundException {        if (!cacheEnabled) {            return new DbForum(forumID);        }        // Cache is enabled.        DbForum forum = (DbForum)forumCache.get(new Long(forumID));        if (forum == null) {            synchronized (Long.toString(forumID).intern()) {                forum = (DbForum)forumCache.get(new Long(forumID));                if (forum == null) {                    forum = new DbForum(forumID);                    forumCache.put(new Long(forumID), forum);                }            }        }        return forum;    }    DbForum getForum(String name) throws ForumNotFoundException {        // If cache is not enabled, do a new lookup of object        if (!cacheEnabled) {            long forumID = DbForumFactory.getInstance().getForumID(name);            return new DbForum(forumID);        }        // Cache is enabled.        Long forumIDLong = (Long)forumIDCache.get(name);        // If id wan't found in cache, load it up and put it there.        if (forumIDLong == null) {            long forumID = DbForumFactory.getInstance().getForumID(name);            forumIDLong = new Long(forumID);            forumIDCache.put(name, forumIDLong);        }        return getForum(forumIDLong.longValue());    }    DbForumThread getForumThread(long threadID) throws ForumThreadNotFoundException {        if (!cacheEnabled) {            return new DbForumThread(threadID);        }        // Cache is enabled.        DbForumThread thread = (DbForumThread)threadCache.get(new Long(threadID));        if (thread == null) {            synchronized (Long.toString(threadID).intern()) {                thread = (DbForumThread)threadCache.get(new Long(threadID));                if (thread == null) {                    thread = new DbForumThread(threadID);                    threadCache.put(new Long(threadID), thread);                }            }        }        return thread;    }    DbForumMessage getMessage(long messageID) throws ForumMessageNotFoundException {        if (!cacheEnabled) {            return new DbForumMessage(messageID);        }        // Cache is enabled.        DbForumMessage message = (DbForumMessage)messageCache.get(new Long(messageID));        if (message == null) {            synchronized (Long.toString(messageID).intern()) {                message = (DbForumMessage)messageCache.get(new Long(messageID));                if (message == null) {                    message = new DbForumMessage(messageID);                    messageCache.put(new Long(messageID), message);                }            }        }        return message;    }    DbAttachment getAttachment(long attachmentID) throws AttachmentNotFoundException {        if (!cacheEnabled) {            return new DbAttachment(attachmentID);        }        // Cache is enabled.        DbAttachment attachment = (DbAttachment)attachmentCache.get(new Long(attachmentID));        if (attachment == null) {            synchronized (Long.toString(attachmentID).intern()) {                attachment = (DbAttachment)attachmentCache.get(new Long(attachmentID));                if (attachment == null) {                    attachment = new DbAttachment(attachmentID);                    attachmentCache.put(new Long(attachmentID), attachment);                }            }        }        return attachment;    }}

⌨️ 快捷键说明

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