Skip to content
Snippets Groups Projects
Select Git revision
  • 1b7ee69ed9ef1b471be86a18b37db82bc950a4f6
  • master default
  • method_check
  • custom_prefix
  • package
  • cookies
  • v2.1.1
  • v2.1.0
  • v2.1.0-rc5
  • v2.1.0-rc4
  • v2.1.0-rc3
  • v2.1.0-rc2
  • v2.1.0-rc1
  • v2.0.7
  • v2.0.6
  • v2.0.5
  • v2.0.4
  • v2.0.3
  • v2.0.2
  • v2.0.1
  • v2.0.0
  • v1.2.8
  • v1.2.7
  • v1.2.6
  • v1.2.5
  • v1.2.4
26 results

store_redis.go

Blame
  • user avatar
    Rohith authored
    - removing the github.com/Sirupsen/logrus logger and replacing zap
    24e68079
    History
    store_redis.go 1.71 KiB
    /*
    Copyright 2015 All rights reserved.
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    
        http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
    */
    
    package main
    
    import (
    	"net/url"
    	"time"
    
    	redis "gopkg.in/redis.v4"
    )
    
    type redisStore struct {
    	client *redis.Client
    }
    
    // newRedisStore creates a new redis store
    func newRedisStore(location *url.URL) (storage, error) {
    	// step: get any password
    	password := ""
    	if location.User != nil {
    		password, _ = location.User.Password()
    	}
    
    	// step: parse the url notation
    	client := redis.NewClient(&redis.Options{
    		Addr:     location.Host,
    		DB:       0,
    		Password: password,
    	})
    
    	return redisStore{
    		client: client,
    	}, nil
    }
    
    // Set adds a token to the store
    func (r redisStore) Set(key, value string) error {
    	if err := r.client.Set(key, value, time.Duration(0)); err.Err() != nil {
    		return err.Err()
    	}
    
    	return nil
    }
    
    // Get retrieves a token from the store
    func (r redisStore) Get(key string) (string, error) {
    	result := r.client.Get(key)
    	if result.Err() != nil {
    		return "", result.Err()
    	}
    
    	return result.String(), nil
    }
    
    // Delete remove the key
    func (r redisStore) Delete(key string) error {
    	return r.client.Del(key).Err()
    }
    
    // Close closes of any open resources
    func (r redisStore) Close() error {
    	if r.client != nil {
    		return r.client.Close()
    	}
    
    	return nil
    }