Java的Spring內實現的mini版記憶體"計數器"功能

rgqancy發表於2016-09-19

工期緊急,不讓用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;
    }

 

相關文章