Realization of real-time updating of Web site cache data

Source: Internet
Author: User
Tags aop

The implementation of a Web site will inevitably need to deal with a large number of memory data, which are mostly due to the performance of the site and solve I/O bottlenecks, such as the Javaeye site's home page ranking data and blog channel rankings and other data, have been cached processing, and is not real-time, In particular, the first page of the ranking data is almost a day will be updated, similarly, QQ and other portal sites also exist a large number of static memory data, so a Web site cache processing is critical, and good architecture needs to do code without intrusion, that is, memory updates, log records and other operations should be "cut" processing, In my later web site development, I generally define a caching class, as follows:

Java code

public static LinkedList<Login> regRecentUser=new LinkedList(); //最近注册人员
 public static LinkedList<Login> regLoginedUser=new LinkedList();//最近登录人员
 public static List<Topic> hotTopic=new ArrayList();//最热发表话题

And will implement the object of the sorting interface, such as the hottest topic, may be subject to a certain number of attributes of the weight distribution to achieve, similar code is as follows:

Java code

public class TopicCompare implements Comparator{
    //热门的话题与顶的数目和评论的数目有关,权重分配比例为4:6 
 public int compare(Object arg0, Object arg1) {
 if((arg0 instanceof Topic) && (arg1 instanceof Topic)){
  double y=((Topic)arg0).getAgreeit()*0.4 + ((Topic)arg0).getCmtnum()*0.6;
  double h=((Topic)arg1).getAgreeit()*0.4 + ((Topic)arg1).getCmtnum()*0.6;
  if(y==h) return 0;
  else if(y>h) return 1;
  else return -1;
 }
 return 0;
 }

}

Cached slice updates generally I like to use spring AOP, simple and practical, general requirements can be met, aspect syntax is more complex, no time to see, hehe

Java code

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

import com.common.cache.WebCacheUtils;
import com.cxlh.dao.hibernate.Login;

public class LoginAdvice implements AfterReturningAdvice {
 public void afterReturning(Object returnValue, Method method, Object[] args,
  Object target) throws Throwable {
 if(method.getName().equalsIgnoreCase("checkUserLogin") && !returnValue.toString().equalsIgnoreCase("null")){
  //最近登录用户缓存更新
  System.out.println("=======update login cache");
  WebCacheUtils.addRegLoginedUser((Login)returnValue);
 }else if(method.getName().equalsIgnoreCase("addUser")){
  //更新最新注册用户缓存
  System.out.println("=======update add user cache");
  WebCacheUtils.addRegRecentUser((Login)args[0]);
 }
 }
}

In this way, the cached data of the most recently logged in and newly registered users are updated in real time by the custom collation, and it is not known whether the real-time cache processing can meet the requirements for large concurrent web sites and has not been tested for performance! Careful with!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.