Java的Spring内实现的mini版内存"计数器"功能

工期紧急,不让用Redis,自己实现了一个Spring内的mini版内存"计数器"功能,很简陋,和业务耦合太紧密,需要改进。

    public Long getCreationCounter() {
        String attributeNameCreationCounter = "CreationCounter";
        Long attributeValueCreationCounter = 0L;
        boolean containsCreationCounter = false;
        
        String attributeNameExpirationDateTime = "ExpirationDateTime";
        Long attributeValueExpirationDateTime = 0L;
        boolean containsExpirationDateTime = false;

        Enumeration<String> attributeNames = ContextUtil.servletContext.getAttributeNames();
        while(attributeNames.hasMoreElements())  
        {  
            String name = attributeNames.nextElement();
            if(!containsCreationCounter && name.equals(attributeNameCreationCounter)){
                containsCreationCounter = true;
            }
            if(!containsExpirationDateTime && name.equals(attributeNameExpirationDateTime)){
                containsExpirationDateTime = true;
            }
            if(containsCreationCounter && containsExpirationDateTime){
                break;
            }
        }

        boolean isExpired = false;
        if(containsExpirationDateTime){//包含超期时间
            Object attributeValueExpirationDateTimeObj = ContextUtil.servletContext.getAttribute(attributeNameExpirationDateTime);
            if(attributeValueExpirationDateTimeObj == null || attributeValueExpirationDateTimeObj.equals("")){//超期时间为空
                isExpired = true;
            } else{//超期时间非空
                attributeValueExpirationDateTime = (Long)ContextUtil.servletContext.getAttribute(attributeNameExpirationDateTime);
                Long nowDateTime = (new Date()).getTime();
                if(nowDateTime < attributeValueExpirationDateTime ){//当前时间小于超期时间,未超期
                    isExpired = false;
                }else{//已超期
                    isExpired = true;
                }
            }
        }else{//不含超期时间
            isExpired = true;
        }
        
        if (isExpired) {//已超期
            attributeValueCreationCounter = getCreationCounter(attributeNameCreationCounter);
            getExpirationDateTimeNew(attributeNameExpirationDateTime);
        } else {//未超期
            if(containsCreationCounter){//包含计数器
                Object attributeValueCreationCounterObj = ContextUtil.servletContext.getAttribute(attributeNameCreationCounter);
                if(attributeValueCreationCounterObj == null || attributeValueCreationCounterObj.equals("")){//计数器为空
                    attributeValueCreationCounter = getCreationCounter(attributeNameCreationCounter);
                } else{//计数器非空
                    attributeValueCreationCounter = (Long)ContextUtil.servletContext.getAttribute(attributeNameCreationCounter);
                }
            } else {//不含计数器
                attributeValueCreationCounter = getCreationCounter(attributeNameCreationCounter);
            }
        }

        return attributeValueCreationCounter;
    }

    //生成新的超期时间,并存入上下文。
    private void getExpirationDateTimeNew(String attributeNameExpirationDateTime) {
        Long attributeValueExpirationDateTime = 0L;
        Date date= new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);//增加1天
        attributeValueExpirationDateTime= calendar.getTime().getTime();
        ContextUtil.servletContext.setAttribute(attributeNameExpirationDateTime, attributeValueExpirationDateTime);
    }
    
    //从数据库查询存证量计数器,并存入上下文。
    private Long getCreationCounter(String attributeNameCreationCounter) {
        Long attributeValueCreationCounter;
        attributeValueCreationCounter = 0L;
        //从数据库读取数据到存证量计数器
        Long creationCounter = ((CreationMapper)storageMapper).getCreationCounter();
        attributeValueCreationCounter = attributeValueCreationCounter + creationCounter;
        ContextUtil.servletContext.setAttribute(attributeNameCreationCounter, attributeValueCreationCounter);
        return attributeValueCreationCounter;
    }

 

posted @ 2016-09-19 10:50  任国强  阅读(1103)  评论(0编辑  收藏  举报