本文整理匯總了Golang中github.com/fiorix/go-redis/redis.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewRedisStore
func NewRedisStore(namespace string, host string) (*RedisStore, error) {
r := &RedisStore{
namespace: namespace,
}
r.client = redis.New(host)
return r, nil
}
示例2: main
func main() {
var err error
cfgfile := flag.String("config", "server.conf", "set config file")
flag.Parse()
Config, err = ReadConfig(*cfgfile)
if err != nil {
log.Fatal(err)
}
// Set up databases
Redis = redis.New(Config.Redis)
MySQL, err = sql.Open("mysql", Config.MySQL)
if err != nil {
log.Fatal(err)
}
// Set up routing and print server info
route()
hello()
// Run HTTP and HTTPS servers
wg := &sync.WaitGroup{}
if Config.HTTP.Addr != "" {
wg.Add(1)
log.Printf("Starting HTTP server on %s", Config.HTTP.Addr)
go func() {
// Use httpxtra's listener to support Unix sockets.
server := http.Server{
Addr: Config.HTTP.Addr,
Handler: httpxtra.Handler{
Logger: logger,
XHeaders: Config.HTTP.XHeaders,
},
}
log.Fatal(httpxtra.ListenAndServe(server))
//wg.Done()
}()
}
if Config.HTTPS.Addr != "" {
wg.Add(1)
log.Printf("Starting HTTPS server on %s", Config.HTTPS.Addr)
go func() {
server := http.Server{
Addr: Config.HTTPS.Addr,
Handler: httpxtra.Handler{Logger: logger},
}
log.Fatal(server.ListenAndServeTLS(
Config.HTTPS.CrtFile, Config.HTTPS.KeyFile))
//wg.Done()
}()
}
wg.Wait()
}
示例3: main
func main() {
redis_client = redis.New("127.0.0.1:6379")
queue_name_re := "([a-zA-Z0-9]+)$"
remux.HandleFunc("^/$", IndexHandler)
remux.HandleFunc("^/q/"+queue_name_re, queueHandler)
server := http.Server{
Addr: ":8080",
Handler: remux.DefaultServeMux,
Logger: logger,
}
server.ListenAndServe()
}
示例4: TestRateLimiter
func TestRateLimiter(t *testing.T) {
counter := struct {
sync.Mutex
n int
}{}
hf := func(w http.ResponseWriter, r *http.Request) {
counter.Lock()
counter.n++
counter.Unlock()
}
kmf := func(r *http.Request) string {
return "rate-limiter-test"
}
rl := &RateLimiter{
Redis: redis.New(),
Max: 2,
Interval: time.Second,
KeyMaker: KeyMakerFunc(kmf),
Handler: http.HandlerFunc(hf),
}
mux := http.NewServeMux()
mux.Handle("/", rl)
s := httptest.NewServer(mux)
defer s.Close()
for i := 0; i < 3; i++ {
resp, err := http.Get(s.URL)
if err != nil {
t.Fatal(err)
}
resp.Body.Close()
if resp.StatusCode == http.StatusServiceUnavailable {
t.Skip("Redis unavailable, cannot proceed")
}
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusForbidden && i != 2 {
t.Fatal(resp.Status)
}
}
lim, _ := strconv.Atoi(resp.Header.Get("X-RateLimit-Limit"))
rem, _ := strconv.Atoi(resp.Header.Get("X-RateLimit-Remaining"))
res, _ := strconv.Atoi(resp.Header.Get("X-RateLimit-Reset"))
switch {
case i == 0 && lim == 2 && rem == 1 && res > 0:
case (i == 1 || i == 2) && lim == 2 && rem == 0 && res > 0:
default:
log.Fatalf("Unexpected values: limit=%d, remaining=%d, reset=%d",
lim, rem, res)
}
}
}
示例5: main
func main() {
configFile := flag.String("c", "%name%.conf", "")
logFile := flag.String("l", "", "")
flag.Usage = func() {
fmt.Println("Usage: %name% [-c %name%.conf] [-l logfile]")
os.Exit(1)
}
flag.Parse()
var err error
config, err := loadConfig(*configFile)
if err != nil {
log.Fatal(err)
}
// Initialize log.
if *logFile != "" {
setLog(*logFile)
}
// Parse templates.
HTML = html.Must(html.ParseGlob(config.TemplatesDir + "/*.html"))
TEXT = text.Must(text.ParseGlob(config.TemplatesDir + "/*.txt"))
// Set up databases.
rc := redis.New(config.DB.Redis)
db, err := sql.Open("mysql", config.DB.MySQL)
if err != nil {
log.Fatal(err)
}
// Set GOMAXPROCS and show server info.
var cpuinfo string
if n := runtime.NumCPU(); n > 1 {
runtime.GOMAXPROCS(n)
cpuinfo = fmt.Sprintf("%d CPUs", n)
} else {
cpuinfo = "1 CPU"
}
log.Printf("%s %s (%s)", APPNAME, VERSION, cpuinfo)
// Start HTTP server.
s := new(httpServer)
s.init(config, rc, db)
go s.ListenAndServe()
go s.ListenAndServeTLS()
// Sleep forever.
select {}
}
示例6: newTestHandler
func newTestHandler(db *freegeoip.DB) http.Handler {
return NewHandler(&HandlerConfig{
Prefix: "/api",
PublicDir: ".",
DB: db,
RateLimiter: RateLimiter{
Redis: redis.New(),
Max: 5,
Interval: time.Second,
KeyMaker: KeyMakerFunc(func(r *http.Request) string {
return "handler-test"
}),
},
})
}
示例7: main
func main() {
cfgfile := flag.String("c", "%name%.conf", "set config file")
flag.Usage = func() {
fmt.Println("Usage: %name% [-c %name%.conf]")
os.Exit(1)
}
flag.Parse()
var err error
cfg, err = LoadConfig(*cfgfile)
if err != nil {
log.Fatal(err)
}
// Set up databases.
Redis = redis.New(cfg.DB.Redis)
MySQL, err = sql.Open("mysql", cfg.DB.MySQL)
if err != nil {
log.Fatal(err)
}
// Print server info and set up HTTP routes.
hello()
RouteHTTP()
// Run HTTP and HTTPS servers.
wg := &sync.WaitGroup{}
if cfg.HTTP.Addr != "" {
wg.Add(1)
go ListenHTTP()
}
if cfg.HTTPS.Addr != "" {
wg.Add(1)
go ListenHTTPS()
}
wg.Wait()
}
示例8: newClient
func (s *redisConfigSource) newClient(db int) *redis.Client {
return redis.New(s.connectionURL(db))
}
示例9: GeoipHandler
// GeoipHandler handles GET on /csv, /xml and /json.
func GeoipHandler() http.HandlerFunc {
db, err := sql.Open("sqlite3", conf.IPDB.File)
if err != nil {
panic(err)
}
_, err = db.Exec("PRAGMA cache_size=" + conf.IPDB.CacheSize)
if err != nil {
panic(err)
}
rc := redis.New(conf.Redis...)
return func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
w.Header().Set("Access-Control-Allow-Origin", "*")
case "OPTIONS":
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Access-Control-Allow-Headers", "X-Requested-With")
w.WriteHeader(200)
return
default:
w.Header().Set("Allow", "GET, OPTIONS")
http.Error(w, http.StatusText(405), 405)
return
}
// GET
// Check quota
var ipkey string
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
ipkey = r.RemoteAddr // support for XHeaders
} else {
ipkey = ip
}
if qcs, err := rc.Get(ipkey); err != nil {
if conf.Debug {
log.Println("Redis error:", err.Error())
}
http.Error(w, http.StatusText(503), 503) // redis down
return
} else if qcs == "" {
if err := rc.Set(ipkey, "1"); err == nil {
rc.Expire(ipkey, conf.Limit.Expire)
}
} else if qc, _ := strconv.Atoi(qcs); qc < conf.Limit.MaxRequests {
rc.Incr(ipkey)
} else {
// Out of quota, soz :(
http.Error(w, http.StatusText(403), 403)
return
}
// Parse URL and build the query.
var ip string
a := strings.SplitN(r.URL.Path, "/", 3)
if len(a) == 3 && a[2] != "" {
// e.g. /csv/google.com
addrs, err := net.LookupHost(a[2])
if err != nil {
http.Error(w, http.StatusText(404), 404)
return
}
ip = addrs[0]
} else {
ip = ipkey
}
geoip, err := GeoipLookup(db, ip)
if err != nil {
http.NotFound(w, r)
return
}
switch a[1][0] {
case 'c':
w.Header().Set("Content-Type", "application/csv")
fmt.Fprintf(w, `"%s","%s","%s","%s","%s","%s",`+
`"%s","%0.4f","%0.4f","%s","%s"`+"\r\n",
geoip.Ip,
geoip.CountryCode, geoip.CountryName,
geoip.RegionCode, geoip.RegionName,
geoip.CityName, geoip.ZipCode,
geoip.Latitude, geoip.Longitude,
geoip.MetroCode, geoip.AreaCode)
case 'j':
resp, err := json.Marshal(geoip)
if err != nil {
if conf.Debug {
log.Println("JSON error:", err.Error())
}
http.NotFound(w, r)
return
}
callback := r.FormValue("callback")
if callback != "" {
w.Header().Set("Content-Type", "text/javascript")
fmt.Fprintf(w, "%s(%s);", callback, resp)
} else {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", resp)
}
case 'x':
//.........這裏部分代碼省略.........
示例10: Setup
func (q *RedisQuota) Setup(args ...string) {
redis.MaxIdleConnsPerAddr = 5000
q.c = redis.New(args...)
q.c.Timeout = time.Duration(1500) * time.Millisecond
}
示例11: redisClear
func redisClear() {
c := redis.New(addr)
c.FlushAll()
}
示例12: init
func (q *redisQuota) init(cf *configFile) {
redis.MaxIdleConnsPerAddr = 5000
q.cf = cf
q.rc = redis.New(cf.Redis...)
q.rc.Timeout = time.Duration(1500) * time.Millisecond
}
示例13: save_to_redis
func save_to_redis(key string, value string) {
redis_client := redis.New("127.0.0.1:6379")
redis_client.Set(key, value)
}
示例14: main
func main() {
cfgfile := flag.String("c", "%name%.conf", "")
keygen := flag.Bool("k", false, "")
flag.Usage = func() {
fmt.Println("Usage: %name% [-k] [-c %name%.conf]")
os.Exit(1)
}
flag.Parse()
if *keygen {
fmt.Println(RandHex(16))
return
}
var err error
cfg, err = LoadConfig(*cfgfile)
if err != nil {
log.Fatal(err)
}
// Set up databases.
Redis = redis.New(cfg.DB.Redis)
MySQL, err = sql.Open("mysql", cfg.DB.MySQL)
if err != nil {
log.Fatal(err)
}
// Load HTML and plain text templates.
LoadTemplates()
// Set up session keys
Session = sessions.NewCookieStore(
[]byte(cfg.Session.AuthKey),
[]byte(cfg.Session.CryptKey),
)
// Public html.
DocumentRoot = http.FileServer(http.Dir(cfg.DocumentRoot))
// Set GOMAXPROCS and show server info.
var cpuinfo string
if n := runtime.NumCPU(); n > 1 {
runtime.GOMAXPROCS(n)
cpuinfo = fmt.Sprintf("%d CPUs", n)
} else {
cpuinfo = "1 CPU"
}
log.Printf("%s v%s (%s)", APPNAME, VERSION, cpuinfo)
// Start email delivery goroutine.
go DeliverEmail()
// Run HTTP and HTTPS servers.
wg := &sync.WaitGroup{}
if cfg.HTTP.Addr != "" {
wg.Add(1)
go ListenHTTP()
}
if cfg.HTTPS.Addr != "" {
wg.Add(1)
go ListenHTTPS()
}
wg.Wait()
}
示例15: Setup
func (q *RedisQuota) Setup(args ...string) {
q.c = redis.New(args...)
q.c.Timeout = time.Duration(800) * time.Millisecond
}