Ver Fonte

store: rename ids to digitsById, exp to idByTime.

Dmitry Chestnykh há 14 anos atrás
pai
commit
60d32e9f9c
1 ficheiros alterados com 19 adições e 18 exclusões
  1. 19 18
      store.go

+ 19 - 18
store.go

@@ -27,17 +27,17 @@ type Store interface {
 // expValue stores timestamp and id of captchas. It is used in the list inside
 // expValue stores timestamp and id of captchas. It is used in the list inside
 // memoryStore for indexing generated captchas by timestamp to enable garbage
 // memoryStore for indexing generated captchas by timestamp to enable garbage
 // collection of expired captchas.
 // collection of expired captchas.
-type expValue struct {
+type idByTimeValue struct {
 	timestamp int64
 	timestamp int64
 	id        string
 	id        string
 }
 }
 
 
 // memoryStore is an internal store for captcha ids and their values.
 // memoryStore is an internal store for captcha ids and their values.
 type memoryStore struct {
 type memoryStore struct {
-	mu  sync.RWMutex
-	ids map[string][]byte
-	exp *list.List
-	// Number of items stored after last collection.
+	mu         sync.RWMutex
+	digitsById map[string][]byte
+	idByTime   *list.List
+	// Number of items stored since last collection.
 	numStored int
 	numStored int
 	// Number of saved items that triggers collection.
 	// Number of saved items that triggers collection.
 	collectNum int
 	collectNum int
@@ -50,8 +50,8 @@ type memoryStore struct {
 // store must be registered with SetCustomStore to replace the default one.
 // store must be registered with SetCustomStore to replace the default one.
 func NewMemoryStore(collectNum int, expiration int64) Store {
 func NewMemoryStore(collectNum int, expiration int64) Store {
 	s := new(memoryStore)
 	s := new(memoryStore)
-	s.ids = make(map[string][]byte)
-	s.exp = list.New()
+	s.digitsById = make(map[string][]byte)
+	s.idByTime = list.New()
 	s.collectNum = collectNum
 	s.collectNum = collectNum
 	s.expiration = expiration
 	s.expiration = expiration
 	return s
 	return s
@@ -59,8 +59,8 @@ func NewMemoryStore(collectNum int, expiration int64) Store {
 
 
 func (s *memoryStore) Set(id string, digits []byte) {
 func (s *memoryStore) Set(id string, digits []byte) {
 	s.mu.Lock()
 	s.mu.Lock()
-	s.ids[id] = digits
-	s.exp.PushBack(expValue{time.Seconds(), id})
+	s.digitsById[id] = digits
+	s.idByTime.PushBack(idByTimeValue{time.Seconds(), id})
 	s.numStored++
 	s.numStored++
 	s.mu.Unlock()
 	s.mu.Unlock()
 	if s.numStored > s.collectNum {
 	if s.numStored > s.collectNum {
@@ -77,15 +77,16 @@ func (s *memoryStore) Get(id string, clear bool) (digits []byte) {
 		s.mu.Lock()
 		s.mu.Lock()
 		defer s.mu.Unlock()
 		defer s.mu.Unlock()
 	}
 	}
-	digits, ok := s.ids[id]
+	digits, ok := s.digitsById[id]
 	if !ok {
 	if !ok {
 		return
 		return
 	}
 	}
 	if clear {
 	if clear {
-		s.ids[id] = nil, false
-		// XXX(dchest) Index (s.exp) will be cleaned when collecting expired
-		// captchas.  Can't clean it here, because we don't store reference to
-		// expValue in the map. Maybe store it?
+		s.digitsById[id] = nil, false
+		// XXX(dchest) Index (s.idByTime) will be cleaned when
+		// collecting expired captchas.  Can't clean it here, because
+		// we don't store reference to expValue in the map.
+		// Maybe store it?
 	}
 	}
 	return
 	return
 }
 }
@@ -95,15 +96,15 @@ func (s *memoryStore) Collect() {
 	s.mu.Lock()
 	s.mu.Lock()
 	defer s.mu.Unlock()
 	defer s.mu.Unlock()
 	s.numStored = 0
 	s.numStored = 0
-	for e := s.exp.Front(); e != nil; {
-		ev, ok := e.Value.(expValue)
+	for e := s.idByTime.Front(); e != nil; {
+		ev, ok := e.Value.(idByTimeValue)
 		if !ok {
 		if !ok {
 			return
 			return
 		}
 		}
 		if ev.timestamp+s.expiration < now {
 		if ev.timestamp+s.expiration < now {
-			s.ids[ev.id] = nil, false
+			s.digitsById[ev.id] = nil, false
 			next := e.Next()
 			next := e.Next()
-			s.exp.Remove(e)
+			s.idByTime.Remove(e)
 			e = next
 			e = next
 		} else {
 		} else {
 			return
 			return