56 Star 149 Fork 40

青苗/gmfs

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
mgostorage.go 1.39 KB
一键复制 编辑 原始数据 按行查看 历史
= 提交于 2015-08-13 11:29 +08:00 . golang 基于 gridFs 的分布式文件服务
package storage
import (
"log"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// MgoStorage objects store and retrieve data using Mongo.
type MgoStorage struct {
// mongo collection where the cache will be stored
Collection *mgo.Collection
}
// New returns a new MgoStorage
func NewMgoStorage(collection *mgo.Collection) *MgoStorage {
return &MgoStorage{
Collection: collection,
}
}
func (self *MgoStorage) Get(key string) (resp []byte, ok bool) {
result := record{}
err := self.Collection.Find(bson.M{"key": key}).One(&result)
if err != nil {
return []byte{}, false
}
return result.Content, true
}
func (self *MgoStorage) Set(key string, content []byte) {
_, err := self.Collection.Upsert(bson.M{"key": key}, &record{
Created: time.Now(),
Updated: time.Now(),
Key: key,
Content: content,
})
if err != nil {
log.Printf("Can't insert record in mongo: %v\n", err)
return
}
return
}
func (self *MgoStorage) Delete(key string) {
err := self.Collection.Remove(bson.M{"key": key})
if err != nil {
log.Printf("Can't remove record: %s", err)
}
}
func (self *MgoStorage) Indexes() {
index := mgo.Index{
Key: []string{"key"},
Unique: true,
DropDups: true,
}
err := self.Collection.EnsureIndex(index)
if err != nil {
log.Printf("Can't ensure index: %s", err)
}
}
type record struct {
Created time.Time
Updated time.Time
Key string
Content []byte
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/jobob/gmfs.git
git@gitee.com:jobob/gmfs.git
jobob
gmfs
gmfs
8375e82f13e4

搜索帮助