Allow querying number cached in ram from c code as well

This commit is contained in:
Kovid Goyal 2021-02-19 09:26:18 +05:30
parent 0341b64748
commit 12604072fc
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 20 additions and 10 deletions

View File

@ -658,6 +658,22 @@ disk_cache_wait_for_write(PyObject *self_, monotonic_t timeout) {
size_t size_t
disk_cache_total_size(PyObject *self) { return ((DiskCache*)self)->total_size; } disk_cache_total_size(PyObject *self) { return ((DiskCache*)self)->total_size; }
size_t
disk_cache_num_cached_in_ram(PyObject *self_) {
DiskCache *self = (DiskCache*)self_;
unsigned long ans = 0;
if (ensure_state(self)) {
mutex(lock);
CacheEntry *tmp, *s;
HASH_ITER(hh, self->entries, s, tmp) {
if (s->written_to_disk && s->data) ans++;
}
mutex(unlock);
}
return ans;
}
#define PYWRAP(name) static PyObject* py##name(DiskCache *self, PyObject *args) #define PYWRAP(name) static PyObject* py##name(DiskCache *self, PyObject *args)
#define PA(fmt, ...) if (!PyArg_ParseTuple(args, fmt, __VA_ARGS__)) return NULL; #define PA(fmt, ...) if (!PyArg_ParseTuple(args, fmt, __VA_ARGS__)) return NULL;
PYWRAP(ensure_state) { PYWRAP(ensure_state) {
@ -776,15 +792,8 @@ remove_from_ram(PyObject *self, PyObject *callable) {
} }
static PyObject* static PyObject*
num_cached_in_ram(DiskCache *self, PyObject *args UNUSED) { num_cached_in_ram(PyObject *self, PyObject *args UNUSED) {
unsigned long ans = 0; return PyLong_FromUnsignedLong(disk_cache_num_cached_in_ram(self));
mutex(lock);
CacheEntry *tmp, *s;
HASH_ITER(hh, self->entries, s, tmp) {
if (s->written_to_disk && s->data) ans++;
}
mutex(unlock);
return PyLong_FromUnsignedLong(ans);
} }
@ -795,7 +804,7 @@ static PyMethodDef methods[] = {
{"add", add, METH_VARARGS, NULL}, {"add", add, METH_VARARGS, NULL},
{"remove", pyremove, METH_VARARGS, NULL}, {"remove", pyremove, METH_VARARGS, NULL},
{"remove_from_ram", remove_from_ram, METH_O, NULL}, {"remove_from_ram", remove_from_ram, METH_O, NULL},
{"num_cached_in_ram", (PyCFunction)num_cached_in_ram, METH_NOARGS, NULL}, {"num_cached_in_ram", num_cached_in_ram, METH_NOARGS, NULL},
{"get", get, METH_VARARGS, NULL}, {"get", get, METH_VARARGS, NULL},
{"wait_for_write", wait_for_write, METH_VARARGS, NULL}, {"wait_for_write", wait_for_write, METH_VARARGS, NULL},
{"size_on_disk", size_on_disk, METH_NOARGS, NULL}, {"size_on_disk", size_on_disk, METH_NOARGS, NULL},

View File

@ -18,6 +18,7 @@ size_t disk_cache_total_size(PyObject *self);
size_t disk_cache_size_on_disk(PyObject *self); size_t disk_cache_size_on_disk(PyObject *self);
void clear_disk_cache(PyObject *self); void clear_disk_cache(PyObject *self);
size_t disk_cache_clear_from_ram(PyObject *self_, bool(matches)(void* data, void *key, unsigned keysz), void*); size_t disk_cache_clear_from_ram(PyObject *self_, bool(matches)(void* data, void *key, unsigned keysz), void*);
size_t disk_cache_num_cached_in_ram(PyObject *self_);
static inline void* disk_cache_malloc_allocator(void *x, size_t sz) { static inline void* disk_cache_malloc_allocator(void *x, size_t sz) {
*((size_t*)x) = sz; *((size_t*)x) = sz;