📄 blogcache.java
字号:
package com.redmoon.blog;
import java.sql.ResultSet;
import java.sql.SQLException;
import cn.js.fan.db.Conn;
import cn.js.fan.base.ObjectCache;
import java.util.Vector;
import java.util.Iterator;
import cn.js.fan.util.StrUtil;
import cn.js.fan.module.photo.PhotoDb;
public class BlogCache extends ObjectCache {
final String NEWBLOGMSG = "NEW_BLOG_MSG";
final String HOTBLOGMSG = "HOT_BLOG_MSG";
final String POSTRANKBLOG = "POST_RANK_BLOG";
final String NEWBLOG = "NEW_BLOG";
final String COMMENDBLOG = "COMMEND_BLOG";
final String ALLBLOG = "ALL_BLOG";
final String NEWBLOGPHOTOS = "NEW_BLOG_PHOTOS";
public BlogCache() {
}
public BlogCache(BlogDb blogDb) {
super(blogDb);
}
public void refreshHomePage() {
try {
rmCache.invalidateGroup(group);
}
catch (Exception e) {
logger.error("refreshHomePage:" + e.getMessage());
}
}
public int[] getNewPhotos(int n) {
// 根据sql语句得出计算总数的sql查询语句
PhotoDb pd = new PhotoDb();
String sql = "select id from " + pd.getTableName() + " ORDER BY addDate desc";
int[] v = null;
try {
v = (int[])rmCache.getFromGroup(NEWBLOGPHOTOS, group);
} catch (Exception e) {
logger.error("getNewPhotos1:" + e.getMessage());
}
// If already in cache, return the count.
if (v != null) {
return v;
}
// Otherwise, we have to load the count from the db.
else {
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
conn.setMaxRows(n);
rs = conn.executeQuery(sql);
v = new int[conn.getRows()];
conn.setFetchSize(n);
int id;
int i = 0;
if (rs!=null) {
while (rs.next()) {
id = rs.getInt(1);
v[i] = id;
i++;
}
}
} catch (SQLException e) {
logger.error("getNewPhotos2:" + e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
// Add the thread count to cache
if (v!=null && v.length>0) {
try {
rmCache.putInGroup(NEWBLOGPHOTOS, group, v);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
return v;
}
}
/**
* 取得博客新发的n篇文章
* @param n int
* @return long[]
*/
public long[] getNewBlogMsgs(int n) {
// 根据sql语句得出计算总数的sql查询语句
String sql = "select id from sq_thread where isBlog=1 order by lydate desc";
long[] v = new long[0];
try {
v = (long[])rmCache.getFromGroup(NEWBLOGMSG, group);
} catch (Exception e) {
logger.error("getTopBlogs:" + e.getMessage());
}
// If already in cache, return the count.
if (v != null) {
return v;
}
// Otherwise, we have to load the count from the db.
else {
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
conn.setMaxRows(n);
rs = conn.executeQuery(sql);
conn.setFetchSize(n);
v = new long[conn.getRows()];
int id;
int i = 0;
while (rs.next()) {
id = rs.getInt(1);
v[i] = id;
i++;
}
} catch (SQLException e) {
logger.error("getTopMsgs:" + e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
// Add the thread count to cache
if (v.length>0) {
try {
rmCache.putInGroup(NEWBLOGMSG, group, v);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
return v;
}
}
/**
* 取得热门贴子
* @param n int
* @return long[]
*/
public long[] getHotBlogMsgs(int n) {
// 根据sql语句得出计算总数的sql查询语句
String sql = "select id from sq_thread where isBlog=1 order by hit desc";
long[] v = new long[0];
try {
v = (long[])rmCache.getFromGroup(HOTBLOGMSG, group);
} catch (Exception e) {
logger.error("getHotBlogMsgs:" + e.getMessage());
}
// If already in cache, return the count.
if (v != null) {
return v;
}
// Otherwise, we have to load the count from the db.
else {
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
conn.setMaxRows(n);
rs = conn.executeQuery(sql);
conn.setFetchSize(n);
v = new long[conn.getRows()];
int id;
int i = 0;
while (rs.next()) {
id = rs.getInt(1);
v[i] = id;
i++;
}
} catch (SQLException e) {
logger.error("getTopMsgs:" + e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if (conn != null) {
conn.close();
conn = null;
}
}
// Add the thread count to cache
if (v.length>0) {
try {
rmCache.putInGroup(HOTBLOGMSG, group, v);
} catch (Exception e) {
logger.error(e.getMessage());
}
}
return v;
}
}
/**
* 发表排行
* @param n int
* @return String[]
*/
public String[] getPostRank(int n) {
// 根据sql语句得出计算总数的sql查询语句
String sql = "select userName from blog_user_config order by msgCount desc";
String[] v = new String[0];
try {
v = (String[])rmCache.getFromGroup(POSTRANKBLOG, group);
} catch (Exception e) {
logger.error("getPostRank:" + e.getMessage());
}
// If already in cache, return the count.
if (v != null) {
return v;
}
// Otherwise, we have to load the count from the db.
else {
Conn conn = new Conn(connname);
ResultSet rs = null;
try {
conn.setMaxRows(n);
rs = conn.executeQuery(sql);
conn.setFetchSize(n);
v = new String[conn.getRows()];
String userName;
int i = 0;
while (rs.next()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -