summaryrefslogtreecommitdiffstats
path: root/models/gpg_key.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-12-11 12:37:04 +0800
committerLauris BH <lauris@nix.lv>2017-12-11 06:37:04 +0200
commitf2e20c81b66e6a937ecdb686f8d1011371433365 (patch)
tree490e5af82aefdd25de5d90225b083ecb3ed11e5f /models/gpg_key.go
parentc082c3bce35d6d5d829a1e516b9bbf45b6d49bdc (diff)
downloadgitea-f2e20c81b66e6a937ecdb686f8d1011371433365.tar.gz
gitea-f2e20c81b66e6a937ecdb686f8d1011371433365.zip
Refactor struct's time to remove unnecessary memory usage (#3142)
* refactor struct's time to remove unnecessary memory usage * use AsTimePtr simple code * fix tests * fix time compare * fix template on gpg * use AddDuration instead of Add
Diffstat (limited to 'models/gpg_key.go')
-rw-r--r--models/gpg_key.go36
1 files changed, 14 insertions, 22 deletions
diff --git a/models/gpg_key.go b/models/gpg_key.go
index 6959f373c9..b7d86c8bfe 100644
--- a/models/gpg_key.go
+++ b/models/gpg_key.go
@@ -17,6 +17,7 @@ import (
"code.gitea.io/git"
"code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/util"
"github.com/go-xorm/xorm"
"github.com/keybase/go-crypto/openpgp"
@@ -26,17 +27,14 @@ import (
// GPGKey represents a GPG key.
type GPGKey struct {
- ID int64 `xorm:"pk autoincr"`
- OwnerID int64 `xorm:"INDEX NOT NULL"`
- KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
- PrimaryKeyID string `xorm:"CHAR(16)"`
- Content string `xorm:"TEXT NOT NULL"`
- Created time.Time `xorm:"-"`
- CreatedUnix int64
- Expired time.Time `xorm:"-"`
- ExpiredUnix int64
- Added time.Time `xorm:"-"`
- AddedUnix int64
+ ID int64 `xorm:"pk autoincr"`
+ OwnerID int64 `xorm:"INDEX NOT NULL"`
+ KeyID string `xorm:"INDEX CHAR(16) NOT NULL"`
+ PrimaryKeyID string `xorm:"CHAR(16)"`
+ Content string `xorm:"TEXT NOT NULL"`
+ CreatedUnix util.TimeStamp `xorm:"created"`
+ ExpiredUnix util.TimeStamp
+ AddedUnix util.TimeStamp
SubsKey []*GPGKey `xorm:"-"`
Emails []*EmailAddress
CanSign bool
@@ -47,17 +45,11 @@ type GPGKey struct {
// BeforeInsert will be invoked by XORM before inserting a record
func (key *GPGKey) BeforeInsert() {
- key.AddedUnix = time.Now().Unix()
- key.ExpiredUnix = key.Expired.Unix()
- key.CreatedUnix = key.Created.Unix()
+ key.AddedUnix = util.TimeStampNow()
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func (key *GPGKey) AfterLoad(session *xorm.Session) {
- key.Added = time.Unix(key.AddedUnix, 0).Local()
- key.Expired = time.Unix(key.ExpiredUnix, 0).Local()
- key.Created = time.Unix(key.CreatedUnix, 0).Local()
-
err := session.Where("primary_key_id=?", key.KeyID).Find(&key.SubsKey)
if err != nil {
log.Error(3, "Find Sub GPGkeys[%d]: %v", key.KeyID, err)
@@ -163,8 +155,8 @@ func parseSubGPGKey(ownerID int64, primaryID string, pubkey *packet.PublicKey, e
KeyID: pubkey.KeyIdString(),
PrimaryKeyID: primaryID,
Content: content,
- Created: pubkey.CreationTime,
- Expired: expiry,
+ CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
+ ExpiredUnix: util.TimeStamp(expiry.Unix()),
CanSign: pubkey.CanSign(),
CanEncryptComms: pubkey.PubKeyAlgo.CanEncrypt(),
CanEncryptStorage: pubkey.PubKeyAlgo.CanEncrypt(),
@@ -236,8 +228,8 @@ func parseGPGKey(ownerID int64, e *openpgp.Entity) (*GPGKey, error) {
KeyID: pubkey.KeyIdString(),
PrimaryKeyID: "",
Content: content,
- Created: pubkey.CreationTime,
- Expired: expiry,
+ CreatedUnix: util.TimeStamp(pubkey.CreationTime.Unix()),
+ ExpiredUnix: util.TimeStamp(expiry.Unix()),
Emails: emails,
SubsKey: subkeys,
CanSign: pubkey.CanSign(),