@@ -727,22 +727,35 @@ app.get("/visitor-badge/:repo", async (c) => {
727
727
const repo = c . req . param ( "repo" ) ;
728
728
const username = repo . split ( "/" ) [ 0 ] ;
729
729
const isGitHub = isGitHubRequest ( c . req . raw ) ;
730
- const redis = Redis . fromEnv ( c . env ) ;
731
730
732
- const viewKey = `views:${ repo } ` ;
733
- let count = parseInt ( ( await redis . get ( viewKey ) ) || "0" ) ;
731
+ let result = await c . env . DB . prepare (
732
+ "SELECT count FROM visitors WHERE repo = ?"
733
+ )
734
+ . bind ( repo )
735
+ . first ( ) ;
736
+
737
+ let count : number = typeof result ?. count === "number" ? result . count : 0 ;
734
738
735
739
if ( isGitHub ) {
736
740
c . executionCtx . waitUntil (
737
741
( async ( ) => {
738
- const [ userResult ] = await Promise . all ( [
739
- c . env . DB . prepare (
740
- `SELECT * FROM github_users WHERE username = ?1 AND (julianday(CURRENT_TIMESTAMP) - julianday(last_updated)) * 24 < 24`
741
- )
742
- . bind ( username )
743
- . first ( ) ,
744
- redis . incr ( viewKey ) ,
745
- ] ) ;
742
+ await c . env . DB . prepare (
743
+ `INSERT INTO visitors (repo, count, last_updated)
744
+ VALUES (?1, 1, CURRENT_TIMESTAMP)
745
+ ON CONFLICT(repo) DO UPDATE SET
746
+ count = count + 1,
747
+ last_updated = CURRENT_TIMESTAMP`
748
+ )
749
+ . bind ( repo )
750
+ . run ( ) ;
751
+
752
+ const userResult = await c . env . DB . prepare (
753
+ `SELECT * FROM github_users
754
+ WHERE username = ?1
755
+ AND (julianday(CURRENT_TIMESTAMP) - julianday(last_updated)) * 24 < 24`
756
+ )
757
+ . bind ( username )
758
+ . first ( ) ;
746
759
747
760
if ( ! userResult ) {
748
761
const githubUser = await getGitHubUser ( username ) ;
@@ -759,11 +772,8 @@ app.get("/visitor-badge/:repo", async (c) => {
759
772
}
760
773
} ) ( )
761
774
) ;
762
- }
763
775
764
- if ( ! count && isGitHub ) {
765
- count = 1 ;
766
- await redis . set ( viewKey , count ) ;
776
+ count ++ ;
767
777
}
768
778
769
779
const style = ( c . req . query ( "style" ) as BadgeStyle [ "style" ] ) || "flat" ;
0 commit comments