mongodb - 确保 MongoDB 以动态时间间隔使数据过期并且调用是幂等的

标签 mongodb go mgo

我正在使用 MongoDB 将用户生成的链接保存在存储器中。用户可以说明他们希望 URL 在过期之前保存多长时间。每个用户 ID 也是唯一的。

理想情况下,我希望我的请求是幂等的。我想调用尽可能多的电话,而不必检查最后一次电话是否有到期值。

我下面的代码似乎给了我:

  • “名称为 creationtime_1 的索引已存在,但选项不同”或
  • 索引不存在。

这是我第一次使用 MongoDB,如果有任何见解,我将不胜感激。我想我也可能对我的代码进行了冗余检查,但我不知道该怎么做

```

//mongo settings
sessionTTL := mgo.Index{
    Key:         []string{"creationtime"},
    Unique:      false,
    DropDups:    false,
    Background:  true,
    ExpireAfter: time.Hour * time.Duration(expires)} // Expire in expire time

// START MONGODB
session, err := mgo.Dial(tokenMongo)
if err != nil {
    return "", err
}
defer session.Close()
//session.SetSafe(&mgo.Safe{})

// Optional. Switch the session to a monotonic behavior.
id := uuid.NewV4().String()

thistime := time.Now().Local()


// find index
err = session.DB("tokenresults").C("tokenurl").Find(bson.M{"id": id}).One(&result)
if err == nil{
    //Drop old values if exist // cant drop if empty
    if err := session.DB("tokenresults").C("tokenurl").DropIndex("creationtime"); err != nil {
        return "", err
    }
}

//add stuff
c := session.DB("tokenresults").C("tokenurl")
err = c.Insert(&TokenUrl{id, tokenstring, thistime}, )
if err != nil {
    return "", err
}

// create index //add session ttl // cant create if exists
if err := session.DB("tokenresults").C("tokenurl").EnsureIndex(sessionTTL); err != nil {
    return "", err
}

```

最佳答案

解决方案

方法is documented :使用日期字段,将值设置为文档过期的日期,创建一个 TTL 索引并将 ExpireAfterSeconds 设置为 0,MongoDB 后台 TTL 清除过程将删除过期的文档。

注意事项

但是,使用 TTL 索引存在一些模糊性。由于对每个即将过期的文档都有一个进程,等待过期时间,然后删除文档,成本太高,MongoDB 选择了不同的解决方案。 There is a background process which checks for expired documents once a minute .因此,无法保证您的文档会在到期时间立即到期,并且文档可能比设定的到期日期长不到 2 分钟(由于过载或其他原因错过了第一次运行,并且只会在下一次删除)运行)。但是请注意,这仅在非常特殊的情况下才会发生。通常,您的文件会在过期后的几分钟内被删除。

说明

我们基本上在这里做的是添加一个字段 ExpirationDate 并创建一个 TTL 索引,该索引设置为检查此到期日期。将此 ExpirationDate 设置为哪个值完全取决于您。使用工厂模式生成 session 或其他内容。

请注意,下面的代码中解释了一些注意事项。

package main

import (
    "flag"
    "fmt"
    "log"
    "time"

    mgo "gopkg.in/mgo.v2"
    "gopkg.in/mgo.v2/bson"
)

const (
    // SESSION_TIMEOUT is a fixed and relatively short
    // timeout for demo purposes
    SESSION_TIMEOUT = 1 * time.Minute
)

// Session is just a sample session struct
// with various session related data and the
// date on which a session should expire.
type Session struct {
    ID             bson.ObjectId `bson:"_id"`
    User           string
    Foo            string
    Bar            string
    ExpirationDate time.Time `bson:"expirationDate"`
}

// NewSession is just a simple helper method to
// return a session with a properly set expiration time
func NewSession(user, foo, bar string) Session {
    // We use a static timeout here.
    // However, you can easily adapt this to use an arbitrary timeout.
    return Session{
        ID:             bson.NewObjectId(),
        User:           user,
        Foo:            foo,
        Bar:            bar,
        ExpirationDate: time.Now().Add(SESSION_TIMEOUT),
    }
}

var (
    mgohost string
    mgoport int
    db      string
    col     string
)

func init() {
    flag.StringVar(&mgohost, "host", "localhost", "MongoDB host")
    flag.IntVar(&mgoport, "port", 27017, "MongoDB port")
    flag.StringVar(&db, "db", "test", "MongoDB database")
    flag.StringVar(&col, "collection", "ttltest", "MongoDB collection")

}

func main() {
    flag.Parse()

    c, err := mgo.Dial(fmt.Sprintf("mongodb://%s:%d/%s", mgohost, mgoport, db))
    if err != nil {
        log.Fatalf("Error connecting to '%s:%d/%s': %s", mgohost, mgoport, db, err)
    }

    // We use a goroutine here in order to make sure
    // that even when EnsureIndex blocks, our program continues
    go func() {
        log.Println("Ensuring sessionTTL index in background")

        // Request a conncetion from the pool
        m := c.DB(db).Session.Copy()
        defer m.Close()

        // We need to set this to 1 as 0 would fail to create the TTL index.
        // See https://github.com/go-mgo/mgo/issues/103 for details
        // This will expire the session within the minute after ExpirationDate.
        //
        // The TTL purging is done once a minute only.
        // See https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
        // for details
        m.DB(db).C(col).EnsureIndex(mgo.Index{ExpireAfter: 1 * time.Second, Key: []string{"expirationDate"}})

        log.Println("sessionTTL index is ready")
    }()

    s := NewSession("mwmahlberg", "foo", "bar")

    if err := c.DB(db).C(col).Insert(&s); err != nil {
        log.Fatalf("Error inserting %#v into %s.%s: %s", s, db, col, err)
    }

    l := Session{}

    if err := c.DB(db).C(col).Find(nil).One(&l); err != nil {
        log.Fatalf("Could not load session from %s.%s: %s", db, col, err)
    }
    log.Printf("Session with ID %s loaded for user '%s' which will expire in %s", l.ID, l.User, time.Until(l.ExpirationDate))
    time.Sleep(2 * time.Minute)

    // Let's check if the session is still there

    if n, err := c.DB(db).C(col).Count(); err != nil {
        log.Fatalf("Error counting documents in %s.%s: %s", db, col, err)
    } else if n > 1 {
        log.Fatalf("Uups! Someting went wrong!")
    }

    log.Println("All sessions were expired.")
}

关于mongodb - 确保 MongoDB 以动态时间间隔使数据过期并且调用是幂等的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48445720/

相关文章:

go - 从 PathError 中获取 errno 值的正确方法

json - Golang & mgo : How to create a generic entity with common fields like _id, 创建时间,最后更新

mongodb - Go 命令返回未找到但在终端中工作

c# - 无法使用 C# 驱动程序过滤 MongoDB 中的日期字段

python - MongoDB 无效文档 : Cannot encode object

mongodb - MongoDB中MapReduce的优化问题

mongodb - 无法在 Mongoose 中保存文档 - 所需的验证器因路径错误而失败

go - golang 是否没有文件结构?

go - 如何在 Go 中列出所有非标准/自定义包?

mongodb - mgo - 查询性能似乎一直很慢(500-650 毫秒)