Skip to content

Generate redis load and store function for protobuffer message.

Notifications You must be signed in to change notification settings

Galaxyobe/protoc-gen-redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protoc-gen-redis

Generate redis load and store function for protobuffer message. use redis string save the message proto data or hash based on github.com/gomodule/redigo/redis

using the following extensions:

  • message options

    • enabled

      enable generate redis load and store function

      default: false

    • storage_type

      store to redis type ,only support string and hash

      default: string

    • storage_codec

      storage codec: proto | json

      default: proto

    • hash_getter

      all hash field getter function

      default: true

    • hash_setter

      all hash field setter function

      default: true

  • message field options

    • storage_field_type

      store to redis type ,only support string and hash

      default: string

    • storage_field_codec

      storage codec: proto | json

      default: proto

    • hash_field_getter

      hash field getter function

      default: true

    • hash_field_setter

      hash field setter function

      default: true

Installing and using

go get -u github.com/galaxyobe/protoc-gen-redis

protoc -I=$GOPATH/src -I=. --go_out=. --redis_out=. *.proto

example

See string_type.proto

syntax = "proto3";
package test;

import "github.com/galaxyobe/protoc-gen-redis/proto/redis.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";

message StringStorageType {
    // enabled generate, default is false
    option (redis.enabled) = true;
    // storage type: string | hash, default is string
    option (redis.storage_type) = "string";
    int32 some_integer = 1;
}

message StringStorageTyp2 {
    // enabled generate, default is false
    option (redis.enabled) = true;
    int32 some_integer = 1;
}

See string_type.redis.go

// Code generated by protoc-gen-gogo. DO NOT EDIT.
// source: string_type.proto

package test

import context "context"
import github_com_gomodule_redigo_redis "github.com/gomodule/redigo/redis"
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import _ "github.com/galaxyobe/protoc-gen-redis/proto"
import _ "github.com/gogo/protobuf/gogoproto"

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// new StringStorageType redis controller with redis pool
func (m *StringStorageType) RedisController(pool *github_com_gomodule_redigo_redis.Pool) *StringStorageTypeRedisController {
	return &StringStorageTypeRedisController{
		pool: pool,
		m:    m,
	}
}

// StringStorageType redis controller
type StringStorageTypeRedisController struct {
	pool *github_com_gomodule_redigo_redis.Pool
	m    *StringStorageType
}

// new StringStorageType redis controller with redis pool
func NewStringStorageTypeRedisController(pool *github_com_gomodule_redigo_redis.Pool) *StringStorageTypeRedisController {
	return &StringStorageTypeRedisController{pool: pool, m: new(StringStorageType)}
}

// get StringStorageType
func (r *StringStorageTypeRedisController) StringStorageType() *StringStorageType {
	return r.m
}

// set StringStorageType
func (r *StringStorageTypeRedisController) SetStringStorageType(m *StringStorageType) {
	r.m = m
}

// store StringStorageType to redis string with context and key
func (r *StringStorageTypeRedisController) Store(ctx context.Context, key string) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// marshal StringStorageType to []byte
	data, err := proto.Marshal(r.m)
	if err != nil {
		return err
	}

	// use redis string store StringStorageType data
	_, err = conn.Do("SET", key, data)

	return err
}

// store StringStorageType to redis string with context, key and ttl expire second
func (r *StringStorageTypeRedisController) StoreWithTTL(ctx context.Context, key string, ttl uint64) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// marshal StringStorageType to []byte
	data, err := proto.Marshal(r.m)
	if err != nil {
		return err
	}

	// use redis string store StringStorageType data with expire second
	_, err = conn.Do("SETEX", key, ttl, data)

	return err
}

// load StringStorageType from redis string with context and key
func (r *StringStorageTypeRedisController) Load(ctx context.Context, key string) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// load data from redis string
	data, err := github_com_gomodule_redigo_redis.Bytes(conn.Do("GET", key))
	if err != nil {
		return err
	}

	// unmarshal data to StringStorageType
	return proto.Unmarshal(data, r.m)
}

// new StringStorageTyp2 redis controller with redis pool
func (m *StringStorageTyp2) RedisController(pool *github_com_gomodule_redigo_redis.Pool) *StringStorageTyp2RedisController {
	return &StringStorageTyp2RedisController{
		pool: pool,
		m:    m,
	}
}

// StringStorageTyp2 redis controller
type StringStorageTyp2RedisController struct {
	pool *github_com_gomodule_redigo_redis.Pool
	m    *StringStorageTyp2
}

// new StringStorageTyp2 redis controller with redis pool
func NewStringStorageTyp2RedisController(pool *github_com_gomodule_redigo_redis.Pool) *StringStorageTyp2RedisController {
	return &StringStorageTyp2RedisController{pool: pool, m: new(StringStorageTyp2)}
}

// get StringStorageTyp2
func (r *StringStorageTyp2RedisController) StringStorageTyp2() *StringStorageTyp2 {
	return r.m
}

// set StringStorageTyp2
func (r *StringStorageTyp2RedisController) SetStringStorageTyp2(m *StringStorageTyp2) {
	r.m = m
}

// store StringStorageTyp2 to redis string with context and key
func (r *StringStorageTyp2RedisController) Store(ctx context.Context, key string) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// marshal StringStorageTyp2 to []byte
	data, err := proto.Marshal(r.m)
	if err != nil {
		return err
	}

	// use redis string store StringStorageTyp2 data
	_, err = conn.Do("SET", key, data)

	return err
}

// store StringStorageTyp2 to redis string with context, key and ttl expire second
func (r *StringStorageTyp2RedisController) StoreWithTTL(ctx context.Context, key string, ttl uint64) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// marshal StringStorageTyp2 to []byte
	data, err := proto.Marshal(r.m)
	if err != nil {
		return err
	}

	// use redis string store StringStorageTyp2 data with expire second
	_, err = conn.Do("SETEX", key, ttl, data)

	return err
}

// load StringStorageTyp2 from redis string with context and key
func (r *StringStorageTyp2RedisController) Load(ctx context.Context, key string) error {
	// redis conn
	conn := r.pool.Get()
	defer conn.Close()

	// load data from redis string
	data, err := github_com_gomodule_redigo_redis.Bytes(conn.Do("GET", key))
	if err != nil {
		return err
	}

	// unmarshal data to StringStorageType
	return proto.Unmarshal(data, r.m)
}

// demo

protoc -I=. -I=$GOPATH/src -I=proto -I=examples --go_out=. --plugin=protoc-gen-redis=./protoc-gen-redis examples/*.proto

About

Generate redis load and store function for protobuffer message.

Resources

Stars

Watchers

Forks

Packages

No packages published