azure openai

This commit is contained in:
Sakurasan
2023-05-27 22:47:32 +08:00
parent 2f7567d23e
commit 9c04122680
5 changed files with 105 additions and 27 deletions

View File

@@ -26,18 +26,18 @@ func LoadKeysCache() {
return
}
for idx, key := range keys {
KeysCache.Set(to.String(idx), key.Key, cache.NoExpiration)
KeysCache.Set(to.String(idx), key, cache.NoExpiration)
}
}
func FromKeyCacheRandomItem() string {
func FromKeyCacheRandomItemKey() Key {
items := KeysCache.Items()
if len(items) == 1 {
return items[to.String(0)].Object.(string)
return items[to.String(0)].Object.(Key)
}
idx := rand.Intn(len(items))
item := items[to.String(idx)]
return item.Object.(string)
return item.Object.(Key)
}
func LoadAuthCache() {

View File

@@ -1,6 +1,9 @@
package store
import "time"
import (
"encoding/json"
"time"
)
type Key struct {
ID uint `gorm:"primarykey" json:"id,omitempty"`
@@ -14,6 +17,11 @@ type Key struct {
UpdatedAt time.Time `json:"updatedAt,omitempty"`
}
func (k Key) ToString() string {
bdate, _ := json.Marshal(k)
return string(bdate)
}
func GetKeyrByName(name string) (*Key, error) {
var key Key
result := db.First(&key, "name = ?", name)
@@ -32,10 +40,11 @@ func GetAllKeys() ([]Key, error) {
}
// 添加记录
func AddKey(apikey, name string) error {
func AddKey(apitype, apikey, name string) error {
key := Key{
Key: apikey,
Name: name,
ApiType: apitype,
Key: apikey,
Name: name,
}
if err := db.Create(&key).Error; err != nil {
return err
@@ -44,6 +53,14 @@ func AddKey(apikey, name string) error {
return nil
}
func CreateKey(k *Key) error {
if err := db.Create(&k).Error; err != nil {
return err
}
LoadKeysCache()
return nil
}
// 删除记录
func DeleteKey(id uint) error {
if err := db.Delete(&Key{}, id).Error; err != nil {