Skip to content

Commit

Permalink
Initial CacheSize vs. CacheMem
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeHandle committed Feb 24, 2015
1 parent 37cf7bd commit b5ed459
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions cache.go
Expand Up @@ -9,6 +9,7 @@ type Cache interface {
has(username string) bool
pull(username string) minecraft.Skin
add(username string, skin minecraft.Skin)
size() uint
memory() uint64
}

Expand Down
8 changes: 7 additions & 1 deletion cache_memory.go
@@ -1,8 +1,9 @@
package main

import (
"github.com/minotar/minecraft"
"time"

"github.com/minotar/minecraft"
)

const (
Expand Down Expand Up @@ -96,6 +97,11 @@ func (c *CacheMemory) add(username string, skin minecraft.Skin) {
})
}

// The exact number of usernames in the map
func (c *CacheMemory) size() uint {
return uint(len(c.Usernames))
}

// The byte size of the cache. Fairly rough... don't really want to venture
// into the land of manual memory management, because there be dragons.
func (c *CacheMemory) memory() uint64 {
Expand Down
4 changes: 4 additions & 0 deletions cache_off.go
Expand Up @@ -25,6 +25,10 @@ func (c *CacheOff) pull(username string) minecraft.Skin {
func (c *CacheOff) add(username string, skin minecraft.Skin) {
}

func (c *CacheOff) size() uint {
return 0
}

func (c *CacheOff) memory() uint64 {
return 0
}
25 changes: 22 additions & 3 deletions cache_redis.go
Expand Up @@ -4,12 +4,13 @@ import (
"bytes"
"errors"
"fmt"
"github.com/fzzy/radix/extra/pool"
"github.com/fzzy/radix/redis"
"github.com/minotar/minecraft"
"image/png"
"strconv"
"strings"

"github.com/fzzy/radix/extra/pool"
"github.com/fzzy/radix/redis"
"github.com/minotar/minecraft"
)

type CacheRedis struct {
Expand Down Expand Up @@ -128,6 +129,24 @@ func (c *CacheRedis) remove(username string) {
err = client.Cmd("DEL", config.Redis.Prefix+username).Err
}

func (c *CacheRedis) size() uint {
var err error
client := c.getFromPool()
if client == nil {
return 0
}
defer c.Pool.CarefullyPut(client, &err)

resp := client.Cmd("DBSIZE")
size, err := resp.Int()
if err != nil {
log.Error(err.Error())
return 0
}

return uint(size)
}

func (c *CacheRedis) memory() uint64 {
var err error
client := c.getFromPool()
Expand Down
9 changes: 6 additions & 3 deletions status.go
Expand Up @@ -34,8 +34,10 @@ type StatusCollector struct {
CacheHits uint
// Number of times skins have failed to be served from the cache.
CacheMisses uint
// Size of cached skins
Cached uint64
// Number of skins in cache.
CacheSize uint
// Size of cache memory.
CacheMem uint64
}

// Unix timestamp the process was booted at.
Expand Down Expand Up @@ -98,7 +100,8 @@ func (s *StatusCollector) Collect() {

s.info.Memory = memstats.Alloc
s.info.Uptime = time.Now().Unix() - s.StartedAt
s.info.Cached = cache.memory()
s.info.CacheSize = cache.size()
s.info.CacheMem = cache.memory()
}

// Increments the request counter for the specific type of request.
Expand Down

0 comments on commit b5ed459

Please sign in to comment.