Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

/v1/shorten 接口连续传入相同的url会 crash #4

Open
scottliyq opened this issue Sep 24, 2014 · 1 comment
Open

/v1/shorten 接口连续传入相同的url会 crash #4

scottliyq opened this issue Sep 24, 2014 · 1 comment

Comments

@scottliyq
Copy link

第二次传入同一个url, 的时候这行代码返回值是nil, 不知道是不是cache的bug。
result.UrlShort = urlcache.Get(urlmd5).(string)

2014/09/24 21:44:14 [C] the request url is /v1/shorten
2014/09/24 21:44:14 [C] Handler crashed with error interface conversion: interface is nil, not string
2014/09/24 21:44:14 [C] /usr/local/go/src/pkg/runtime/panic.c:248
2014/09/24 21:44:14 [C] /private/var/folders/00/0sdwh000h01000cxqpysvccm0035qk/T/makerelease530016500/go/src/pkg/runtime/iface.goc:292
2014/09/24 21:44:14 [C] /private/var/folders/00/0sdwh000h01000cxqpysvccm0035qk/T/makerelease530016500/go/src/pkg/runtime/iface.goc:280
2014/09/24 21:44:14 [C] /Users/1/kanbox/gocode/src/shorturl/controllers/short.go:36
2014/09/24 21:44:14 [C] /Users/1/kanbox/gocode/src/github.com/astaxie/beego/router.go:721
2014/09/24 21:44:14 [C] /usr/local/go/src/pkg/net/http/server.go:1673
2014/09/24 21:44:14 [C] /usr/local/go/src/pkg/net/http/server.go:1174
2014/09/24 21:44:14 [C] /usr/local/go/src/pkg/runtime/proc.c:1445

@astaxie astaxie closed this as completed Sep 24, 2014
@astaxie astaxie reopened this Sep 24, 2014
@crazygit
Copy link

恩,现在还是有这个问题。
仔细看了一下代码,不是cache的问题,而是使用cache在这里保存数据不太合适。

原代码使用缓存来保存数据,并且不清理缓存。

// interval为0, 表示不会清理过期的缓存
func init() {
    urlcache, _ = cache.NewCache("memory", `{"interval":0}`)
}

但是在设置保存短链接时,由于设置的有效时间为0,导致刚刚设置的缓存信息就立即就失效,

    err := urlcache.Put(urlmd5, result.UrlShort, 0)

下次再从缓存里面读取时,由于缓存信息已经失效,故直接返回nil。但是代码里面没有做相应的检查,因此报出上面的错误

    if urlcache.IsExist(urlmd5) {
        result.UrlShort = urlcache.Get(urlmd5).(string)
    }

解决方法有两种:

  1. 将设置缓存的过期的时间调大,这样就可以减少上面错误的发生
  2. 修改代码,当从缓存中取到的信息过期时,再自动重新生成一个

上面两种解决方式都是临时解决方案,最好的解决办法还是根据应用的场景,换一个持久存储代替使用缓存保存信息更加恰当。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants