基于内存消耗的自动缓存驱逐 [英] Automatic cache eviction based on memory consumption

查看:107
本文介绍了基于内存消耗的自动缓存驱逐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一个高效的LRU缓存,可以根据空闲内存自动清除项目。

现在只有两件事情可以想到:




  • 使用 gosigar 进行投票

  • 或设置固定的最大值并定期检查 runtime.ReadMemStats



是否有其他方法? memcached 如何做到这一点?

解决方案

我实现了轮询系统内存统计数据每1秒。



请参阅: https:// github.com/eaigner/last



在Linux上阅读内存统计信息:

 导入(
系统调用


func ReadSysMemStats(s * MemStats)错误{
if s == nil {
返回nil
}
var info syscall.Sysinfo_t
err:= syscall.Sysinfo(&info)
if err!= nil {
return err
}

s.Total = info.Totalram
s.Free = info.Freeram
s.Used = s.Total - s.Free

$ n
}

在Darwin / OSX上

  / * 
#include< mach / mach.h>
#include< mach / mach_host.h>
* /
进口C

进口(
fmt
不安全


func readSysMemStats(s * SysMemStats)错误{
if s == nil {
return nil
}
var vm_pagesize C.vm_size_t
var vm_stat C.vm_statistics_data_t
var count C.mach_msg_type_number_t = C.HOST_VM_INFO_COUNT

host_port:= C.host_t(C.mach_host_self())

C.host_page_size(host_port,& vm_pagesize )

status:= C.host_statistics(
host_port,
C.HOST_VM_INFO,
C.host_info_t(unsafe.Pointer(& vm_stat)),
& count)

if status!= C.KERN_SUCCESS {
return fmt.Errorf(could not get vm statistics:%d,status)
}

//以字节为单位的数据
free:= uint64(vm_stat.free_count)
active:= uint64(vm_stat.active_count)
inactive:= uint64(vm_stat.inactive_count )
wired:= uint64(vm_stat.wire_count)
pagesize:= uint64(vm_pagesize)

s.Used =(active + inactive + wired)* pagesize
s.Free = free * pagesize
s.Total = s.Used + s.Free

return nil
}


I want to implement an efficient LRU cache that automatically evicts items based on free memory.

Right now only 2 things come to mind:

  • Poll with gosigar
  • or set a fixed maximum and periodically check runtime.ReadMemStats.

Are there any other ways? How does memcached do it?

解决方案

I implemented it polling the system memory statistics every 1 second.

See: https://github.com/eaigner/last

Read memory stats on Linux:

import (
    "syscall"
)

func ReadSysMemStats(s *MemStats) error {
    if s == nil {
        return nil
    }
    var info syscall.Sysinfo_t
    err := syscall.Sysinfo(&info)
    if err != nil {
        return err
    }

    s.Total = info.Totalram
    s.Free = info.Freeram
    s.Used = s.Total - s.Free

    return nil
}

And on Darwin/OSX

/*
#include <mach/mach.h>
#include <mach/mach_host.h>
*/
import "C"

import (
    "fmt"
    "unsafe"
)

func readSysMemStats(s *SysMemStats) error {
    if s == nil {
        return nil
    }
    var vm_pagesize C.vm_size_t
    var vm_stat C.vm_statistics_data_t
    var count C.mach_msg_type_number_t = C.HOST_VM_INFO_COUNT

    host_port := C.host_t(C.mach_host_self())

    C.host_page_size(host_port, &vm_pagesize)

    status := C.host_statistics(
        host_port,
        C.HOST_VM_INFO,
        C.host_info_t(unsafe.Pointer(&vm_stat)),
        &count)

    if status != C.KERN_SUCCESS {
        return fmt.Errorf("could not get vm statistics: %d", status)
    }

    // Stats in bytes
    free := uint64(vm_stat.free_count)
    active := uint64(vm_stat.active_count)
    inactive := uint64(vm_stat.inactive_count)
    wired := uint64(vm_stat.wire_count)
    pagesize := uint64(vm_pagesize)

    s.Used = (active + inactive + wired) * pagesize
    s.Free = free * pagesize
    s.Total = s.Used + s.Free

    return nil
}

这篇关于基于内存消耗的自动缓存驱逐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆