晕死,超过今天最大附件了,源码项目明天再发吧。
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Discuz.Common;
using Discuz.Forum;
using System.Web;
using System.Web.Caching;
namespace Discuz.Plugin
{
public class TopShow
{
private static int topiccount = 10;
private static int piccount = 6;
private static int minutes = 10;
private const string NEWTOPIC_KEY = "newtopic";
private const string NEWREPLY_KEY = "newreply";
private const string HOTTOPIC_KEY = "hottopic";
private const string NEWPIC_KEY = "newpic";
public static string SetTopShow(int topic,int pic,int min)
{
if (topic != topiccount || pic != piccount || min != minutes)
{
RemoveCache();
topiccount = topic;
piccount = pic;
minutes = min;
}
return "<!--当前显示帖子数为" + topiccount + ",显示图片数为" + piccount + ",缓存时间为" + minutes + "分钟-->";
}
public static string GetNewTopic()
{
if (HttpContext.Current.Cache[NEWTOPIC_KEY] == null)
{
string SQL_NEWTOPIC = "SELECT TOP " + topiccount + " tid, title FROM " + BaseConfigFactory.GetTablePrefix + "topics ORDER BY postdatetime DESC";
TopicCache(SQL_NEWTOPIC, NEWTOPIC_KEY);
}
return (string)HttpContext.Current.Cache[NEWTOPIC_KEY];
}
public static string GetNewReply()
{
if (HttpContext.Current.Cache[NEWREPLY_KEY] == null)
{
string SQL_NEWREPLY = "SELECT TOP " + topiccount + " tid, title FROM " + BaseConfigFactory.GetTablePrefix + "topics WHERE (postdatetime<>lastpost) ORDER BY lastpost DESC";
TopicCache(SQL_NEWREPLY, NEWREPLY_KEY);
}
return (string)HttpContext.Current.Cache[NEWREPLY_KEY];
}
public static string GetHotTopic()
{
if (HttpContext.Current.Cache[HOTTOPIC_KEY] == null)
{
string SQL_HOTTOPIC = "SELECT TOP " + topiccount + " tid, title FROM " + BaseConfigFactory.GetTablePrefix + "topics ORDER BY views DESC";
TopicCache(SQL_HOTTOPIC, HOTTOPIC_KEY);
}
return (string)HttpContext.Current.Cache[HOTTOPIC_KEY];
}
public static void TopicCache(string sql,string cachekey)
{
StringBuilder stb = new StringBuilder();
SqlDataReader reader = Database.ExecuteReader(CommandType.Text, sql);
if (reader != null)
{
while (reader.Read())
{
stb.Append("<div style='width:200px; text-overflow:ellipsis;overflow:hidden'><nobr><a href=\"showtopic-");
stb.Append(reader[0].ToString().Trim());
stb.Append(".aspx\" target=\"_blank\">");
stb.Append(reader[1].ToString().Trim());
stb.Append("</a></nobr></div>");
}
}
HttpContext.Current.Cache.Insert(cachekey, stb.ToString(), null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration);
}
public static string GetNewPic()
{
if (HttpContext.Current.Cache[NEWPIC_KEY] == null)
{
StringBuilder spics = new StringBuilder();
StringBuilder stexts = new StringBuilder();
StringBuilder slinks = new StringBuilder();
SqlDataReader reader = Database.ExecuteReader(CommandType.Text, "SELECT TOP " + piccount + " " + BaseConfigFactory.GetTablePrefix + "attachments.filename, " + BaseConfigFactory.GetTablePrefix + "topics.title, " + BaseConfigFactory.GetTablePrefix + "attachments.tid FROM " + BaseConfigFactory.GetTablePrefix + "attachments INNER JOIN " + BaseConfigFactory.GetTablePrefix + "topics ON " + BaseConfigFactory.GetTablePrefix + "attachments.tid = " + BaseConfigFactory.GetTablePrefix + "topics.tid WHERE (" + BaseConfigFactory.GetTablePrefix + "attachments.attachment LIKE '%.jpg%') ORDER BY " + BaseConfigFactory.GetTablePrefix + "attachments.postdatetime DESC");
if (reader != null)
{
while (reader.Read())
{
spics.Append("|upload/" + reader[0].ToString().Trim().Replace("\\", "/"));
stexts.Append("|" + reader[1].ToString().Trim());
slinks.Append("|showtopic-" + reader[2].ToString().Trim() + ".aspx");
}
}
string pics = "var pics ='" + spics.ToString().Remove(0, 1) + "';";
string texts = "var texts ='" + stexts.ToString().Remove(0, 1) + "';";
string links = "var links ='" + slinks.ToString().Remove(0, 1) + "';";
HttpContext.Current.Cache.Insert(NEWPIC_KEY, pics + texts + links, null, DateTime.Now.AddMinutes(minutes), Cache.NoSlidingExpiration);
}
return (string)HttpContext.Current.Cache[NEWPIC_KEY];
}
public static void RemoveCache()
{
if (HttpContext.Current.Cache[NEWTOPIC_KEY] != null)
HttpContext.Current.Cache.Remove(NEWTOPIC_KEY);
if (HttpContext.Current.Cache[NEWREPLY_KEY] != null)
HttpContext.Current.Cache.Remove(NEWREPLY_KEY);
if (HttpContext.Current.Cache[HOTTOPIC_KEY] != null)
HttpContext.Current.Cache.Remove(HOTTOPIC_KEY);
if (HttpContext.Current.Cache[NEWPIC_KEY] != null)
HttpContext.Current.Cache.Remove(NEWPIC_KEY);
}
}
}