{"id":18185120,"url":"https://github.com/pete911/examples-redigo","last_synced_at":"2025-04-01T23:31:14.728Z","repository":{"id":150290280,"uuid":"73908692","full_name":"pete911/examples-redigo","owner":"pete911","description":"golang redigo examples","archived":false,"fork":false,"pushed_at":"2020-12-07T18:23:50.000Z","size":4,"stargazers_count":86,"open_issues_count":0,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-16T01:11:22.810Z","etag":null,"topics":["go","golang","redigo","redis"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pete911.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-16T10:23:01.000Z","updated_at":"2023-04-21T03:57:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"49acf43c-2a86-476b-8ea2-76fe6145ac9f","html_url":"https://github.com/pete911/examples-redigo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pete911%2Fexamples-redigo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pete911%2Fexamples-redigo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pete911%2Fexamples-redigo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pete911%2Fexamples-redigo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pete911","download_url":"https://codeload.github.com/pete911/examples-redigo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246706669,"owners_count":20820785,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","redigo","redis"],"created_at":"2024-11-02T22:22:29.532Z","updated_at":"2025-04-01T23:31:14.720Z","avatar_url":"https://github.com/pete911.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# examples-redigo\n\nExamples on using redigo library. First file contains initialized redis pool\nthat is stored in `Pool` variable. Pool connects by default `localhost:6379`.\nThis can be changed by passing `REDIS_HOST` environment variable.\n\n**redis/pool.go** file\n\n```go\npackage redis\n\nimport (\n    \"github.com/gomodule/redigo/redis\"\n    \"os\"\n    \"os/signal\"\n    \"syscall\"\n    \"time\"\n)\n\nvar (\n    Pool *redis.Pool\n)\n\nfunc init() {\n    redisHost := os.Getenv(\"REDIS_HOST\")\n    if redisHost == \"\" {\n        redisHost = \":6379\"\n    }\n    Pool = newPool(redisHost)\n    cleanupHook()\n}\n\nfunc newPool(server string) *redis.Pool {\n\n    return \u0026redis.Pool{\n\n        MaxIdle:     3,\n        IdleTimeout: 240 * time.Second,\n\n        Dial: func() (redis.Conn, error) {\n            c, err := redis.Dial(\"tcp\", server)\n            if err != nil {\n                return nil, err\n            }\n            return c, err\n        },\n\n        TestOnBorrow: func(c redis.Conn, t time.Time) error {\n            _, err := c.Do(\"PING\")\n            return err\n        },\n    }\n}\n\nfunc cleanupHook() {\n\n    c := make(chan os.Signal, 1)\n    signal.Notify(c, os.Interrupt)\n    signal.Notify(c, syscall.SIGTERM)\n    signal.Notify(c, syscall.SIGKILL)\n    go func() {\n        \u003c-c\n        Pool.Close()\n        os.Exit(0)\n    }()\n}\n```\n\nUtil file provides basic (most common) redis operations. This is supposed to be\nan example not full list of all operations.\n\n**redis/util.go** file\n\n```go\npackage redis\n\nimport (\n    \"fmt\"\n    \"github.com/gomodule/redigo/redis\"\n)\n\nfunc Ping() error {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    _, err := redis.String(conn.Do(\"PING\"))\n    if err != nil {\n        return fmt.Errorf(\"cannot 'PING' db: %v\", err)\n    }\n    return nil\n}\n\nfunc Get(key string) ([]byte, error) {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    var data []byte\n    data, err := redis.Bytes(conn.Do(\"GET\", key))\n    if err != nil {\n        return data, fmt.Errorf(\"error getting key %s: %v\", key, err)\n    }\n    return data, err\n}\n\nfunc Set(key string, value []byte) error {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    _, err := conn.Do(\"SET\", key, value)\n    if err != nil {\n        v := string(value)\n        if len(v) \u003e 15 {\n            v = v[0:12] + \"...\"\n        }\n        return fmt.Errorf(\"error setting key %s to %s: %v\", key, v, err)\n    }\n    return err\n}\n\nfunc Exists(key string) (bool, error) {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    ok, err := redis.Bool(conn.Do(\"EXISTS\", key))\n    if err != nil {\n        return ok, fmt.Errorf(\"error checking if key %s exists: %v\", key, err)\n    }\n    return ok, err\n}\n\nfunc Delete(key string) error {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    _, err := conn.Do(\"DEL\", key)\n    return err\n}\n\nfunc GetKeys(pattern string) ([]string, error) {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    iter := 0\n    keys := []string{}\n    for {\n        arr, err := redis.Values(conn.Do(\"SCAN\", iter, \"MATCH\", pattern))\n        if err != nil {\n            return keys, fmt.Errorf(\"error retrieving '%s' keys\", pattern)\n        }\n\n        iter, _ = redis.Int(arr[0], nil)\n        k, _ := redis.Strings(arr[1], nil)\n        keys = append(keys, k...)\n\n        if iter == 0 {\n            break\n        }\n    }\n\n    return keys, nil\n}\n\nfunc Incr(counterKey string) (int, error) {\n\n    conn := Pool.Get()\n    defer conn.Close()\n\n    return redis.Int(conn.Do(\"INCR\", counterKey))\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpete911%2Fexamples-redigo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpete911%2Fexamples-redigo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpete911%2Fexamples-redigo/lists"}