{"action":"created","issue":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/1030","repository_url":"https://api.github.com/repos/jinzhu/gorm","labels_url":"https://api.github.com/repos/jinzhu/gorm/issues/1030/labels{/name}","comments_url":"https://api.github.com/repos/jinzhu/gorm/issues/1030/comments","events_url":"https://api.github.com/repos/jinzhu/gorm/issues/1030/events","html_url":"https://github.com/jinzhu/gorm/issues/1030","id":156133803,"number":1030,"title":"How to implement Redis/Memcache or other caching at a pre-query level?","user":{"login":"benguild","id":1185659,"avatar_url":"https://avatars.githubusercontent.com/u/1185659?v=3","gravatar_id":"","url":"https://api.github.com/users/benguild","html_url":"https://github.com/benguild","followers_url":"https://api.github.com/users/benguild/followers","following_url":"https://api.github.com/users/benguild/following{/other_user}","gists_url":"https://api.github.com/users/benguild/gists{/gist_id}","starred_url":"https://api.github.com/users/benguild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benguild/subscriptions","organizations_url":"https://api.github.com/users/benguild/orgs","repos_url":"https://api.github.com/users/benguild/repos","events_url":"https://api.github.com/users/benguild/events{/privacy}","received_events_url":"https://api.github.com/users/benguild/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-05-22T04:11:18Z","updated_at":"2016-05-25T15:33:49Z","closed_at":null,"body":"Question:\r\nHas anyone managed to implement a Redis/Memcache layer at the pre-query level? For example, using: https://godoc.org/github.com/jinzhu/gorm#Callback\r\n\r\nI'm trying to figure out how to implement this as cleanly as possible, because certain objects never change after being INSERTed into the database. Fetching them from memory drastically increases speeds."},"comment":{"url":"https://api.github.com/repos/jinzhu/gorm/issues/comments/221614429","html_url":"https://github.com/jinzhu/gorm/issues/1030#issuecomment-221614429","issue_url":"https://api.github.com/repos/jinzhu/gorm/issues/1030","id":221614429,"user":{"login":"benguild","id":1185659,"avatar_url":"https://avatars.githubusercontent.com/u/1185659?v=3","gravatar_id":"","url":"https://api.github.com/users/benguild","html_url":"https://github.com/benguild","followers_url":"https://api.github.com/users/benguild/followers","following_url":"https://api.github.com/users/benguild/following{/other_user}","gists_url":"https://api.github.com/users/benguild/gists{/gist_id}","starred_url":"https://api.github.com/users/benguild/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benguild/subscriptions","organizations_url":"https://api.github.com/users/benguild/orgs","repos_url":"https://api.github.com/users/benguild/repos","events_url":"https://api.github.com/users/benguild/events{/privacy}","received_events_url":"https://api.github.com/users/benguild/received_events","type":"User","site_admin":false},"created_at":"2016-05-25T15:33:49Z","updated_at":"2016-05-25T15:33:49Z","body":"For this particular issue (#1030), I think the best solution I can come up with for the moment is to write some sort of pseudo-SQL to use as the key for caching particular queries. (That, or to GUID each WHERE statement in the entire project, which seems like it could be unfriendly.) I've considered creating an enum for queries so that the indexes could be looked up in a table:\r\n\r\n```\r\ntype DatabaseQueryCacheKey int\r\n\r\nconst (\r\n        FetchUserRowByUsername Base = iota\r\n        FetchUserRowByUserID\r\n        FetchUserRowByAge\r\n        // etc.\r\n)\r\n```\r\n\r\nThen for example constructing keys like this (probably using a function to avoid having to pass the formula):\r\n```\r\nfunc CacheKey(model interface {}, queryCacheKey DatabaseQueryCacheKey, whereValues [] string) string {\r\n    return model.UserModel{}.TableName()+\":\"+strconv.Itoa(queryCacheKey)+\":\"+strings.Join(whereValues, \":\");\r\n\r\n}\r\n```\r\n\r\n... Something like that seems like it could work? As long as you left blank names \"_\" for any \"retired\" `DatabaseQueryCacheKey` values, it seems future-proof enough."}}
{"actor":{"display_login":"benguild"}}