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 AddCacheAddress(address, password string)
- func DelCacheAddress(key string)
- func GetCachePrefix() []byte
- func NewMarsharlDriverSql(driverName string, fun driversqlType)
- func SetCacheAddress(keys []string, password string)
- func SetCachePrefix(str string)
- func SetCacheWithPool(pool *redis.Pool)
- func SetDebug(b bool)
- func SetDefaultCacheDb(db int)
- func SetReadConnectName(name string)
- func SetWriteConnectName(name string)
- func UseHashCache(b bool)
- type Cache
- type CacheHook
- func (self *CacheHook) All(out interface{}) error
- func (self *CacheHook) AllOnCache(out interface{}) error
- func (self *CacheHook) Ca(key interface{}) *CacheHook
- func (self *CacheHook) Count() (int64, error)
- func (self *CacheHook) CountOnCache() (int64, error)
- func (self *CacheHook) Db(name string) *CacheHook
- func (self *CacheHook) Delete() (err error)
- func (self *CacheHook) DeleteOnCache() error
- func (self *CacheHook) Filter(name string, val interface{}) *CacheHook
- func (self *CacheHook) GetCacheKey() string
- func (self *CacheHook) Incrby(key string, val int64) (ret int64, err error)
- func (self *CacheHook) Incry(key string) (val int64, err error)
- func (self *CacheHook) Limit(page, step int) *CacheHook
- func (self *CacheHook) Objects(mode Module, param ...string) *CacheHook
- func (self *CacheHook) One() error
- func (self *CacheHook) OneOnCache() error
- func (self *CacheHook) Orderby(order ...string) *CacheHook
- func (self *CacheHook) Query() (Rows, error)
- func (self *CacheHook) Save() (isnew bool, id int64, err error)
- func (self CacheHook) SaveToCache() error
- func (self *CacheHook) Set(key string, value interface{}) (err error)
- type CacheModule
- type CacheRows
- 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 DBHook
- func (self *DBHook) All(out interface{}) error
- func (self *DBHook) Change(name string, val interface{}) *DBHook
- func (self *DBHook) Count() (int64, error)
- func (self *DBHook) Db(name string) *DBHook
- func (self *DBHook) Delete() (int64, error)
- func (self *DBHook) DoesNotExist() error
- func (self *DBHook) Existed() *DBHook
- func (self *DBHook) Field(name string) reflect.Value
- func (self *DBHook) Filter(name string, val interface{}) *DBHook
- func (self *DBHook) FilterOr(name string, val interface{}) *DBHook
- func (self *DBHook) Filters(filters map[string]interface{}) *DBHook
- func (self *DBHook) Limit(page, steq int) *DBHook
- func (self *DBHook) Objects(mode Module, params ...string) *DBHook
- func (self *DBHook) One() error
- func (self *DBHook) Orderby(names ...string) *DBHook
- func (self *DBHook) Query() (Rows, error)
- func (self *DBHook) Save() (bool, int64, error)
- func (self *DBHook) Set(name string, val interface{}) *DBHook
- type Database
- type FuncParam
- type ModeRows
- 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
- 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 RedisCache
- func (c *RedisCache) ConnGet() redis.Conn
- func (c *RedisCache) Del(key string) (bool, error)
- func (c *RedisCache) Exists(key string) (bool, error)
- func (c *RedisCache) Get(key string) ([]byte, error)
- func (c *RedisCache) Hget(key, field string) ([]byte, error)
- func (c *RedisCache) Hincrby(key, field string, n int64) (int64, error)
- func (c *RedisCache) Hmset(key string, maping interface{}) (err error)
- func (c *RedisCache) Hset(key, field string, b []byte) (bool, error)
- func (c *RedisCache) Incrby(key string, n int64) (int64, error)
- func (c *RedisCache) Keys(key string) (keys []string, err error)
- func (c *RedisCache) Set(key string, b []byte) (err error)
- type Rows
- 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 (
CacheServer = map[string]Cache{}
)
var Debug = log.New(os.Stdout, "ORM-DEBUG ", log.Lshortfile|log.LstdFlags)
var ErrDoesNotExist = errors.New("DoesNotExist")
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 OpenSyncDelete = false
var OpenSyncUpdate = false
var Pool *redis.Pool = nil
var SqlSyncHook = make(chan string, 1000)
Functions ¶
func DelCacheAddress ¶
func DelCacheAddress(key string)
func GetCachePrefix ¶
func GetCachePrefix() []byte
func NewMarsharlDriverSql ¶
func NewMarsharlDriverSql(driverName string, fun driversqlType)
func SetCacheAddress ¶
func SetCachePrefix ¶
func SetCachePrefix(str string)
func SetCacheWithPool ¶
func SetDefaultCacheDb ¶
func SetDefaultCacheDb(db int)
func UseHashCache ¶
func UseHashCache(b bool)
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) // contains filtered or unexported methods }
func GetCacheClient ¶
func GetCacheConn ¶
func GetCachePool ¶
func GetCachePool() Cache
type CacheHook ¶
type CacheHook struct { Cache DBHook Object *DBHook CacheFileds []string CacheNames []string // contains filtered or unexported fields }
func (*CacheHook) AllOnCache ¶
func (*CacheHook) CountOnCache ¶
func (*CacheHook) DeleteOnCache ¶
func (*CacheHook) GetCacheKey ¶
func (*CacheHook) OneOnCache ¶
func (CacheHook) SaveToCache ¶
type CacheModule ¶
type CacheModule struct{ CacheHook }
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 DBHook ¶
func (*DBHook) DoesNotExist ¶
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 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 RedisCache ¶
func NewRedisCacheWithRedisPool ¶
func NewRedisCacheWithRedisPool(pool *redis.Pool) *RedisCache
func (*RedisCache) ConnGet ¶
func (c *RedisCache) ConnGet() redis.Conn
func (*RedisCache) Hincrby ¶
func (c *RedisCache) Hincrby(key, field string, n int64) (int64, error)
func (*RedisCache) Hmset ¶
func (c *RedisCache) Hmset(key string, maping interface{}) (err error)
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{})