예제 #1
0
// cacheLoad: load an already cached puzzle entry.  Returns
// whether the entry was found in the cache.
func (pe *puzzleEntry) cacheLoad() bool {
	var bytes []byte
	body := func(tx redis.Conn) (err error) {
		bytes, err = redis.Bytes(tx.Do("GET", pe.key()))
		if err == redis.ErrNil {
			return nil
		}
		if err != nil {
			err = fmt.Errorf("Cache failure loading puzzleEntry %q: %v", pe.PuzzleId, err)
		}
		return
	}
	rdExecute(body)
	if len(bytes) == 0 {
		return false
	}
	var spe *puzzleEntry
	err := json.Unmarshal(bytes, &spe)
	if err != nil {
		panic(fmt.Errorf("Failed to unmarshal puzzleEntry %q: %v", pe.PuzzleId, err))
	}
	if spe.PuzzleId != pe.PuzzleId {
		panic(fmt.Errorf("Cached puzzleEntry (id: %q) found for puzzle %q!",
			spe.PuzzleId, pe.PuzzleId))
	}
	*pe = *spe
	return true
}
예제 #2
0
// loadLastStep: load the last cached step to the active puzzle
func (s *Session) loadLastStep() {
	var bytes []byte
	body := func(tx redis.Conn) (err error) {
		bytes, err = redis.Bytes(tx.Do("LINDEX", s.stepsKey(), -1))
		if err != nil {
			err = fmt.Errorf("Cache failure reading last step: %v", err)
		}
		return
	}
	rdExecute(body)
	s.Puzzle = s.unmarshalPuzzle(bytes)
}
예제 #3
0
// cacheLoadEntry: load the specified session entry from the
// cache.  This will fail if the entry doesn't already exist.
func (s *Session) cacheLoadEntry(index int) {
	var bytes []byte
	body := func(tx redis.Conn) (err error) {
		bytes, err = redis.Bytes(tx.Do("LINDEX", s.entryKey(), index))
		if err == redis.ErrNil {
			return fmt.Errorf("No entry %d for session %q", index, s.sid)
		}
		if err != nil {
			return fmt.Errorf("Cache error on lookup of entry %d for session %q: %v",
				index, s.sid, err)
		}
		return
	}
	rdExecute(body)
	s.unmarshalEntry(index, bytes)
}
예제 #4
0
// cacheLoadInfo: load the session info from the cache.
func (s *Session) cacheLoadInfo() {
	var bytes []byte
	body := func(tx redis.Conn) (err error) {
		bytes, err = redis.Bytes(tx.Do("GET", s.infoKey()))
		if err == redis.ErrNil {
			return nil
		}
		if err != nil {
			return fmt.Errorf("Cache error on lookup of info for session %q: %v", s.sid, err)
		}
		return
	}
	rdExecute(body)
	if len(bytes) == 0 {
		return
	}
	s.unmarshalInfo(bytes)
}