mirror of
https://github.com/elyby/chrly.git
synced 2024-12-23 21:50:03 +05:30
#1: Implement UuidsStorage in Redis
This commit is contained in:
parent
533afcc689
commit
f3690686ec
@ -3,6 +3,7 @@ package db
|
|||||||
import (
|
import (
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
|
"github.com/elyby/chrly/api/mojang/queue"
|
||||||
"github.com/elyby/chrly/interfaces"
|
"github.com/elyby/chrly/interfaces"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,6 +14,7 @@ type StorageFactory struct {
|
|||||||
type RepositoriesCreator interface {
|
type RepositoriesCreator interface {
|
||||||
CreateSkinsRepository() (interfaces.SkinsRepository, error)
|
CreateSkinsRepository() (interfaces.SkinsRepository, error)
|
||||||
CreateCapesRepository() (interfaces.CapesRepository, error)
|
CreateCapesRepository() (interfaces.CapesRepository, error)
|
||||||
|
CreateMojangUuidsRepository() (queue.UuidsStorage, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory *StorageFactory) CreateFactory(backend string) RepositoriesCreator {
|
func (factory *StorageFactory) CreateFactory(backend string) RepositoriesCreator {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/elyby/chrly/api/mojang/queue"
|
||||||
"github.com/elyby/chrly/interfaces"
|
"github.com/elyby/chrly/interfaces"
|
||||||
"github.com/elyby/chrly/model"
|
"github.com/elyby/chrly/model"
|
||||||
)
|
)
|
||||||
@ -26,6 +27,10 @@ func (f FilesystemFactory) CreateCapesRepository() (interfaces.CapesRepository,
|
|||||||
return &filesStorage{path: path.Join(f.BasePath, f.CapesDirName)}, nil
|
return &filesStorage{path: path.Join(f.BasePath, f.CapesDirName)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (f FilesystemFactory) CreateMojangUuidsRepository() (queue.UuidsStorage, error) {
|
||||||
|
panic("implement me")
|
||||||
|
}
|
||||||
|
|
||||||
func (f FilesystemFactory) validateFactoryConfig() error {
|
func (f FilesystemFactory) validateFactoryConfig() error {
|
||||||
if f.BasePath == "" {
|
if f.BasePath == "" {
|
||||||
return &ParamRequired{"basePath"}
|
return &ParamRequired{"basePath"}
|
||||||
|
51
db/redis.go
51
db/redis.go
@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
"github.com/mediocregopher/radix.v2/redis"
|
"github.com/mediocregopher/radix.v2/redis"
|
||||||
"github.com/mediocregopher/radix.v2/util"
|
"github.com/mediocregopher/radix.v2/util"
|
||||||
|
|
||||||
|
"github.com/elyby/chrly/api/mojang/queue"
|
||||||
"github.com/elyby/chrly/interfaces"
|
"github.com/elyby/chrly/interfaces"
|
||||||
"github.com/elyby/chrly/model"
|
"github.com/elyby/chrly/model"
|
||||||
)
|
)
|
||||||
@ -27,7 +29,20 @@ type RedisFactory struct {
|
|||||||
|
|
||||||
// TODO: maybe we should manually return connection to the pool?
|
// TODO: maybe we should manually return connection to the pool?
|
||||||
|
|
||||||
|
// TODO: Why isn't a pointer used here?
|
||||||
func (f RedisFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error) {
|
func (f RedisFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error) {
|
||||||
|
return f.createInstance()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f RedisFactory) CreateCapesRepository() (interfaces.CapesRepository, error) {
|
||||||
|
panic("capes repository not supported for this storage type")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f RedisFactory) CreateMojangUuidsRepository() (queue.UuidsStorage, error) {
|
||||||
|
return f.createInstance()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f RedisFactory) createInstance() (*redisDb, error) {
|
||||||
connection, err := f.getConnection()
|
connection, err := f.getConnection()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -36,10 +51,6 @@ func (f RedisFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error
|
|||||||
return &redisDb{connection}, nil
|
return &redisDb{connection}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f RedisFactory) CreateCapesRepository() (interfaces.CapesRepository, error) {
|
|
||||||
panic("capes repository not supported for this storage type")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f RedisFactory) getConnection() (*pool.Pool, error) {
|
func (f RedisFactory) getConnection() (*pool.Pool, error) {
|
||||||
if f.connection == nil {
|
if f.connection == nil {
|
||||||
if f.Host == "" {
|
if f.Host == "" {
|
||||||
@ -89,7 +100,9 @@ type redisDb struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const accountIdToUsernameKey = "hash:username-to-account-id"
|
const accountIdToUsernameKey = "hash:username-to-account-id"
|
||||||
|
const mojangUsernameToUuidKey = "hash:mojang-username-to-uuid"
|
||||||
|
|
||||||
|
// TODO: return connection to the pool after usage
|
||||||
func (db *redisDb) FindByUsername(username string) (*model.Skin, error) {
|
func (db *redisDb) FindByUsername(username string) (*model.Skin, error) {
|
||||||
return findByUsername(username, db.getConn())
|
return findByUsername(username, db.getConn())
|
||||||
}
|
}
|
||||||
@ -110,6 +123,14 @@ func (db *redisDb) RemoveByUsername(username string) error {
|
|||||||
return removeByUsername(username, db.getConn())
|
return removeByUsername(username, db.getConn())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *redisDb) GetUuid(username string) (string, error) {
|
||||||
|
return findMojangUuidByUsername(username, db.getConn())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (db *redisDb) StoreUuid(username string, uuid string) {
|
||||||
|
storeMojangUuid(username, uuid, db.getConn())
|
||||||
|
}
|
||||||
|
|
||||||
func (db *redisDb) getConn() util.Cmder {
|
func (db *redisDb) getConn() util.Cmder {
|
||||||
conn, _ := db.conn.Get()
|
conn, _ := db.conn.Get()
|
||||||
return conn
|
return conn
|
||||||
@ -221,6 +242,28 @@ func save(skin *model.Skin, conn util.Cmder) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func findMojangUuidByUsername(username string, conn util.Cmder) (string, error) {
|
||||||
|
response := conn.Cmd("HGET", mojangUsernameToUuidKey, strings.ToLower(username))
|
||||||
|
if response.IsType(redis.Nil) {
|
||||||
|
return "", &queue.ValueNotFound{}
|
||||||
|
}
|
||||||
|
|
||||||
|
data, _ := response.Str()
|
||||||
|
parts := strings.Split(data, ":")
|
||||||
|
timestamp, _ := strconv.ParseInt(parts[1], 10, 64)
|
||||||
|
storedAt := time.Unix(timestamp, 0)
|
||||||
|
if storedAt.Add(time.Hour * 24 * 30).Before(time.Now()) {
|
||||||
|
return "", &queue.ValueNotFound{}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts[0], nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func storeMojangUuid(username string, uuid string, conn util.Cmder) {
|
||||||
|
value := uuid + ":" + strconv.FormatInt(time.Now().Unix(), 10)
|
||||||
|
conn.Cmd("HSET", mojangUsernameToUuidKey, strings.ToLower(username), value)
|
||||||
|
}
|
||||||
|
|
||||||
func buildUsernameKey(username string) string {
|
func buildUsernameKey(username string) string {
|
||||||
return "username:" + strings.ToLower(username)
|
return "username:" + strings.ToLower(username)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user