Documentation ¶
Overview ¶
Package consistent provides a consistent hashing function.
Consistent hashing is often used to distribute requests to a changing set of servers. For example, say you have some cache servers cacheA, cacheB, and cacheC. You want to decide which cache server to use to look up information on a user.
You could use a typical hash table and hash the user id to one of cacheA, cacheB, or cacheC. But with a typical hash table, if you add or remove a server, almost all keys will get remapped to different results, which basically could bring your service to a grinding halt while the caches get rebuilt.
With a consistent hash, adding or removing a server drastically reduces the number of keys that get remapped.
Read more about consistent hashing on wikipedia: http://en.wikipedia.org/wiki/Consistent_hashing
Index ¶
- Variables
- func GetRedisClient(key string) *redis.Client
- func NewMarsharlDriverSql(driverName string, fun driversqlType)
- func SetCachePrefix(str string)
- func SetDebug(b bool)
- func SetDefaultCacheDb(db int)
- type Cache
- type CacheModule
- func (self *CacheModule) All() ([]interface{}, error)
- func (self *CacheModule) AllOnCache() ([]interface{}, error)
- func (self *CacheModule) Ca(key interface{}) *CacheModule
- func (self *CacheModule) Count() (int64, error)
- func (self *CacheModule) CountOnCache() (int64, error)
- func (self *CacheModule) Db(name string) *CacheModule
- func (self *CacheModule) DeleteOnCache() error
- func (self *CacheModule) Filter(name string, val interface{}) *CacheModule
- func (self *CacheModule) GetCacheKey() string
- func (self *CacheModule) Incrby(key string, val int64) (ret int64, err error)
- func (self *CacheModule) Incry(key string) (val int64, err error)
- func (self *CacheModule) Limit(page, step int) *CacheModule
- func (self *CacheModule) Objects(mode Module) *CacheModule
- func (self *CacheModule) One() error
- func (self *CacheModule) OneOnCache() error
- func (self *CacheModule) Orderby(order ...string) *CacheModule
- func (self *CacheModule) Save() (isnew bool, id int64, err error)
- func (self *CacheModule) SaveToCache() error
- func (self *CacheModule) Set(key string, val interface{}) (err error)
- type CacheModuleInteerface
- type Consistent
- func (c *Consistent) Add(elt string)
- func (c *Consistent) Get(name string) (string, error)
- func (c Consistent) GetCircle() map[uint32]string
- func (c *Consistent) GetN(name string, n int) ([]string, error)
- func (c *Consistent) GetTwo(name string) (string, string, error)
- func (c *Consistent) Members() []string
- func (c *Consistent) Remove(elt string)
- func (c *Consistent) Set(elts []string)
- type Database
- type Module
- type ModuleToSql
- type MysqlModeToSql
- func (self MysqlModeToSql) Count() (sql string, val []interface{})
- func (self MysqlModeToSql) Delete() (sql string, val []interface{})
- func (self MysqlModeToSql) Insert() (sql string, val []interface{})
- func (self MysqlModeToSql) Instance(param ParamsInterface)
- func (self MysqlModeToSql) Select() (sql string, val []interface{})
- func (self MysqlModeToSql) Update() (sql string, val []interface{})
- type Object
- func (self *Object) All() ([]interface{}, error)
- func (self *Object) Change(name string, val interface{}) *Object
- func (self *Object) Count() (int64, error)
- func (self *Object) Db(name string) *Object
- func (self *Object) Delete() (int64, error)
- func (self *Object) Filter(name string, val interface{}) *Object
- func (self *Object) FilterOr(name string, val interface{}) *Object
- func (self *Object) Filters(filters map[string]interface{}) *Object
- func (self *Object) Limit(page, steq int) *Object
- func (self *Object) Objects(mode Module) *Object
- func (self *Object) One() error
- func (self *Object) Orderby(names ...string) *Object
- func (self *Object) Save() (bool, int64, error)
- type Params
- func (self *Params) All() (rows *sql.Rows, err error)
- func (self *Params) Change(name string, val interface{})
- func (self *Params) Count() (int64, error)
- func (self *Params) Db(name string) *Params
- func (self *Params) Delete() (res sql.Result, err error)
- func (self *Params) Filter(name string, val interface{}) *Params
- func (self *Params) FilterOr(name string, val interface{}) *Params
- func (self Params) GetFields() []string
- func (self Params) GetLimit() [2]int
- func (self Params) GetOr() []ParmaField
- func (self Params) GetOrLen() int
- func (self Params) GetOrder() []string
- func (self Params) GetSet() []ParmaField
- func (self Params) GetSetLen() int
- func (self Params) GetTableName() string
- func (self Params) GetWhere() []ParmaField
- func (self Params) GetWhereLen() int
- func (self *Params) Init()
- func (self *Params) Limit(page, step int) *Params
- func (self *Params) One(vals ...interface{}) error
- func (self *Params) Save() (bool, int64, error)
- func (self *Params) SetField(fields ...string)
- func (self *Params) SetTable(tbname string)
- type ParamsInterface
- type ParmaField
- type PostgressModeToSql
- func (self PostgressModeToSql) Count() (sql string, val []interface{})
- func (self PostgressModeToSql) Delete() (sql string, val []interface{})
- func (self PostgressModeToSql) Insert() (sql string, val []interface{})
- func (self PostgressModeToSql) Instance(param ParamsInterface)
- func (self PostgressModeToSql) Select() (sql string, val []interface{})
- func (self PostgressModeToSql) Update() (sql string, val []interface{})
- type SqliteModeToSql
- func (self SqliteModeToSql) Count() (sql string, val []interface{})
- func (self SqliteModeToSql) Delete() (sql string, val []interface{})
- func (self SqliteModeToSql) Insert() (sql string, val []interface{})
- func (self SqliteModeToSql) Instance(param ParamsInterface)
- func (self SqliteModeToSql) Select() (sql string, val []interface{})
- func (self SqliteModeToSql) Update() (sql string, val []interface{})
Constants ¶
This section is empty.
Variables ¶
var CacheConsistent = NewConsistent()
var Debug = log.New(os.Stdout, "ORM-DEBUG", log.Lshortfile|log.LstdFlags)
var ErrEmptyCircle = errors.New("empty circle")
ErrEmptyCircle is the error returned when trying to get an element when nothing has been added to hash.
var (
ErrKeyNotExist = errors.New("keys not exists")
)
var Error = log.New(os.Stdout, "ORM-ERROR", log.Lshortfile|log.LstdFlags)
var NULL_LIMIT = [2]int{0, 0}
var (
RedisServer = map[string]*redis.Client{}
)
Functions ¶
func GetRedisClient ¶
func NewMarsharlDriverSql ¶
func NewMarsharlDriverSql(driverName string, fun driversqlType)
func SetCachePrefix ¶
func SetCachePrefix(str string)
func SetDefaultCacheDb ¶
func SetDefaultCacheDb(db int)
Types ¶
type Cache ¶
type Cache interface { Set(key string, b []byte) error Get(key string) ([]byte, error) Keys(key string) ([]string, error) //Incy(key string) (int64, error) Incrby(key string, n int64) (int64, error) Hset(key, field string, b []byte) (bool, error) Hmset(key string, maping interface{}) error Hget(key, field string) ([]byte, error) Hincrby(key, filed string, n int64) (int64, error) Exists(key string) (bool, error) Del(key string) (bool, error) }
func GetCacheConn ¶
type CacheModule ¶
type CacheModule struct { Cache Object CacheFileds []string CacheNames []string // contains filtered or unexported fields }
func CacheMode ¶
func CacheMode(mode Module) (cache *CacheModule)
func (*CacheModule) All ¶
func (self *CacheModule) All() ([]interface{}, error)
func (*CacheModule) AllOnCache ¶
func (self *CacheModule) AllOnCache() ([]interface{}, error)
func (*CacheModule) Ca ¶
func (self *CacheModule) Ca(key interface{}) *CacheModule
func (*CacheModule) Count ¶
func (self *CacheModule) Count() (int64, error)
func (*CacheModule) CountOnCache ¶
func (self *CacheModule) CountOnCache() (int64, error)
func (*CacheModule) Db ¶
func (self *CacheModule) Db(name string) *CacheModule
func (*CacheModule) DeleteOnCache ¶
func (self *CacheModule) DeleteOnCache() error
func (*CacheModule) Filter ¶
func (self *CacheModule) Filter(name string, val interface{}) *CacheModule
func (*CacheModule) GetCacheKey ¶
func (self *CacheModule) GetCacheKey() string
func (*CacheModule) Incrby ¶
func (self *CacheModule) Incrby(key string, val int64) (ret int64, err error)
func (*CacheModule) Limit ¶
func (self *CacheModule) Limit(page, step int) *CacheModule
func (*CacheModule) Objects ¶
func (self *CacheModule) Objects(mode Module) *CacheModule
func (*CacheModule) One ¶
func (self *CacheModule) One() error
func (*CacheModule) OneOnCache ¶
func (self *CacheModule) OneOnCache() error
func (*CacheModule) Orderby ¶
func (self *CacheModule) Orderby(order ...string) *CacheModule
func (*CacheModule) SaveToCache ¶
func (self *CacheModule) SaveToCache() error
func (*CacheModule) Set ¶
func (self *CacheModule) Set(key string, val interface{}) (err error)
type CacheModuleInteerface ¶
type CacheModuleInteerface interface { Objects(Module) CacheModuleInteerface Ca(interface{}) CacheModuleInteerface //一致性hash 默认处理方式 Db(string) CacheModuleInteerface //数据库连接 Filter(name string, val interface{}) CacheModuleInteerface GetCacheKey() string Incrby(string, int64) (int64, error) Incry(string) (int64, error) Set(string, interface{}) error Save() (bool, int64, error) One() error SaveToCache() error All() ([]interface{}, error) AllCache() ([]interface{}, error) }
type Consistent ¶
type Consistent struct { NumberOfReplicas int sync.RWMutex // contains filtered or unexported fields }
Consistent holds the information about the members of the consistent hash circle.
func NewConsistent ¶
func NewConsistent() *Consistent
New creates a new Consistent object with a default setting of 20 replicas for each entry.
To change the number of replicas, set NumberOfReplicas before adding entries.
func (*Consistent) Add ¶
func (c *Consistent) Add(elt string)
Add inserts a string element in the consistent hash.
func (*Consistent) Get ¶
func (c *Consistent) Get(name string) (string, error)
Get returns an element close to where name hashes to in the circle.
func (Consistent) GetCircle ¶
func (c Consistent) GetCircle() map[uint32]string
func (*Consistent) GetN ¶
func (c *Consistent) GetN(name string, n int) ([]string, error)
GetN returns the N closest distinct elements to the name input in the circle.
func (*Consistent) GetTwo ¶
func (c *Consistent) GetTwo(name string) (string, string, error)
GetTwo returns the two closest distinct elements to the name input in the circle.
func (*Consistent) Members ¶
func (c *Consistent) Members() []string
func (*Consistent) Remove ¶
func (c *Consistent) Remove(elt string)
Remove removes an element from the hash.
func (*Consistent) Set ¶
func (c *Consistent) Set(elts []string)
Set sets all the elements in the hash. If there are existing elements not present in elts, they will be removed.
type Database ¶
func NewDatabase ¶
type ModuleToSql ¶
type MysqlModeToSql ¶
type MysqlModeToSql struct {
Params ParamsInterface
}
func (MysqlModeToSql) Count ¶
func (self MysqlModeToSql) Count() (sql string, val []interface{})
func (MysqlModeToSql) Delete ¶
func (self MysqlModeToSql) Delete() (sql string, val []interface{})
func (MysqlModeToSql) Insert ¶
func (self MysqlModeToSql) Insert() (sql string, val []interface{})
func (MysqlModeToSql) Instance ¶
func (self MysqlModeToSql) Instance(param ParamsInterface)
func (MysqlModeToSql) Select ¶
func (self MysqlModeToSql) Select() (sql string, val []interface{})
func (MysqlModeToSql) Update ¶
func (self MysqlModeToSql) Update() (sql string, val []interface{})
type Object ¶
type Params ¶
type Params struct {
// contains filtered or unexported fields
}
*
传参解析
*
func (Params) GetOr ¶
func (self Params) GetOr() []ParmaField
func (Params) GetSet ¶
func (self Params) GetSet() []ParmaField
func (Params) GetTableName ¶
func (Params) GetWhere ¶
func (self Params) GetWhere() []ParmaField
func (Params) GetWhereLen ¶
type ParamsInterface ¶
type ParamsInterface interface { GetOrLen() int GetWhereLen() int GetSetLen() int GetOr() []ParmaField GetWhere() []ParmaField GetSet() []ParmaField GetFields() []string GetOrder() []string GetLimit() [2]int GetTableName() string }
type ParmaField ¶
type ParmaField struct {
// contains filtered or unexported fields
}
type PostgressModeToSql ¶
type PostgressModeToSql struct {
Params ParamsInterface
}
func (PostgressModeToSql) Count ¶
func (self PostgressModeToSql) Count() (sql string, val []interface{})
func (PostgressModeToSql) Delete ¶
func (self PostgressModeToSql) Delete() (sql string, val []interface{})
func (PostgressModeToSql) Insert ¶
func (self PostgressModeToSql) Insert() (sql string, val []interface{})
func (PostgressModeToSql) Instance ¶
func (self PostgressModeToSql) Instance(param ParamsInterface)
func (PostgressModeToSql) Select ¶
func (self PostgressModeToSql) Select() (sql string, val []interface{})
func (PostgressModeToSql) Update ¶
func (self PostgressModeToSql) Update() (sql string, val []interface{})
type SqliteModeToSql ¶
type SqliteModeToSql struct {
Params ParamsInterface
}
func (SqliteModeToSql) Count ¶
func (self SqliteModeToSql) Count() (sql string, val []interface{})
func (SqliteModeToSql) Delete ¶
func (self SqliteModeToSql) Delete() (sql string, val []interface{})
func (SqliteModeToSql) Insert ¶
func (self SqliteModeToSql) Insert() (sql string, val []interface{})
func (SqliteModeToSql) Instance ¶
func (self SqliteModeToSql) Instance(param ParamsInterface)
func (SqliteModeToSql) Select ¶
func (self SqliteModeToSql) Select() (sql string, val []interface{})
func (SqliteModeToSql) Update ¶
func (self SqliteModeToSql) Update() (sql string, val []interface{})