Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): cache USo-format JSON to disk
This will help resolve the issue when multiple users request the index, but it hasn't been generated yet, which then causes performance issues.
- Loading branch information
Showing
3 changed files
with
56 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package models | ||
|
||
import ( | ||
"time" | ||
|
||
"userstyles.world/modules/database" | ||
"userstyles.world/modules/errors" | ||
) | ||
|
||
type USoFormat struct { | ||
Username string `json:"an"` | ||
Name string `json:"n"` | ||
Category string `json:"c"` | ||
Screenshot string `json:"sn"` | ||
UpdatedAt int64 `json:"u"` | ||
TotalInstalls int64 `json:"t"` | ||
WeeklyInstalls int64 `json:"w"` | ||
ID uint `json:"i"` | ||
} | ||
|
||
type USoStyles []USoFormat | ||
|
||
func (s *USoStyles) Query() error { | ||
lastWeek := time.Now().Add(-time.Hour * 24 * 7) | ||
|
||
stmt := "styles.id, styles.name, styles.user_id, u.username, " | ||
stmt += "styles.category, strftime('%s', styles.created_at) as created_at, " | ||
stmt += "strftime('%s', styles.updated_at) as updated_at, " | ||
stmt += "printf('https://userstyles.world/api/style/preview/%d.webp', styles.id) as screenshot, " | ||
stmt += "(select count(*) from stats where stats.style_id = styles.id) as total_installs, " | ||
stmt += "(select count(*) from stats where stats.style_id = styles.id and updated_at > ? and created_at < ?) as weekly_installs" | ||
|
||
err := database.Conn. | ||
Table("styles"). | ||
Select(stmt, lastWeek, lastWeek). | ||
Joins("join users u on u.id = styles.user_id"). | ||
Find(&s). | ||
Error | ||
if err != nil { | ||
return errors.ErrStylesNotFound | ||
} | ||
|
||
return nil | ||
} |