缓存的处理

缓存的处理
/**
 * $Id$
 * Copyright 2009-2010 Oak Pacific Interactive. All rights reserved.
 */
package com.xiaonei.wap.fuxi.service.impl;

import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import com.renren.xcache.XCache;
import com.xiaonei.wap.framework.cache.XCacheManager2;
import com.xiaonei.wap.fuxi.dao.IUserAccessFlowDAO;
import com.xiaonei.wap.fuxi.model.UserAccessFlow;
import com.xiaonei.wap.fuxi.service.IUserAccessService;

/**
 * 
 * @author <a href="mailto:[email protected]">王侨</a>
 * @createTime 2011-7-21下午02:37:12
 */
public class UserAccessServiceImpl extends BaseBusinessService implements IUserAccessService,
        InitializingBean {

    /**
     * Logger for this class
     */
    private final Log logger = LogFactory.getLog(UserAccessServiceImpl.class);

    private static final String CACHE_TYPE_USER_ACCESS_FLOW = "user_access_flow";

    // user access缓存时间,默认1天
    private int userAccessDefaultCacheTime = XCache.EXPIRE_DAY * 1;

    @Autowired
    private IUserAccessFlowDAO userAccessFlowDAO;

    /**
     * 基础方法,根据 userId获取访问基本信息
     * 
     * @param userId
     * @return
     */
    private UserAccessFlow getUserAccessFlow(int userId) {
        if (userId < 0) {
            return null;
        }
        //从缓存中取
        XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
        UserAccessFlow userAccessFlow = cache.get(String.valueOf(userId), UserAccessFlow.class);
        if (userAccessFlow == null) {
            userAccessFlow = loadUserAccessFlowFromDb(userId);
        }
        return userAccessFlow;
    }

    /**
     * 从db中取一条userAccessFlow,放入缓存
     * 
     * @param userId
     * @return
     */
    private UserAccessFlow loadUserAccessFlowFromDb(int userId) {
        UserAccessFlow userAccessFlow = userAccessFlowDAO.find(userId);
        if (userAccessFlow == null) {
            return null;
        }
        String key = String.valueOf(userId);
        //存入缓存
        XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
        cache.save(key, userAccessFlow, UserAccessFlow.class, userAccessDefaultCacheTime);
        return userAccessFlow;
    }

    /**
     * 基础方法,向db中存入一条记录
     * 
     * @param userId
     * @param access
     * @return
     */
    private long createUserAccess(int userId, int access) {
        if (userId < 0) {
            //取不到userId返回新版
            return 0;
        }

        //第一次访问,db中无记录,将access,存入db
        UserAccessFlow userAccessFlow = buildUserAccessFlow(userId, access);
        if (userAccessFlow == null) {
            return 0;
        }

        long result = 0;
        try {
            result = userAccessFlowDAO.create(userAccessFlow);
            //新增数据后删除缓存
            //XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
            //cache.delete(String.valueOf(userId), UserAccessFlow.class);
            
        } catch (Exception e) {
            logger.error("creatUserAccessFlow error, userId:" + userAccessFlow.getUserId(), e);
        }

        return result;
    }

    /**
     * 返回access的值0,旧版;1新版
     * 
     * @param userId
     * @param access
     */
    @Override
    public int getUserAccess(int userId, int access) {
        if (userId < 0) {
            //取不到userId返回新版
            return 1;
        }
        UserAccessFlow userAccess = getUserAccessFlow(userId);

        // 第一次访问,db中无记录,将access,存入db
        if (userAccess == null) {
            createUserAccess(userId, access);
            return access;
        }

        return userAccess.getAccess();
    }

    @Override
    public int updateUserAccess(int userId, int access) {
        if (userId < 0) {
            //取不到userId返回新版
            return 0;
        }

        UserAccessFlow userAccessFlow = buildUserAccessFlow(userId, access);
        if (userAccessFlow == null) {
            return 0;
        }

        int result = 0;
        try {
            //更新数据之前删除缓存
            //XCacheManager2 cache = XCacheManager2.getCacheManager(CACHE_TYPE_USER_ACCESS_FLOW);
            //cache.delete(String.valueOf(userId), UserAccessFlow.class);
            result = userAccessFlowDAO.update(userAccessFlow);
        } catch (Exception e) {
            logger.error("updateUserAccessFlow error, userId:" + userAccessFlow.getUserId(), e);
        }
        return result;
    }

    /**
     * @param userId
     * @param access
     * @return
     */
    private UserAccessFlow buildUserAccessFlow(int userId, int access) {
        UserAccessFlow userAccessFlow = new UserAccessFlow();
        userAccessFlow.setAccess(access);
        userAccessFlow.setUserId(userId);
        userAccessFlow.setAddTime(new Date());
        return userAccessFlow;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.notNull(userAccessFlowDAO, "userAccessFlowDAO is required!");
    }

}

你可能感兴趣的:(缓存)