You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.9 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. /*
  2. * cache.c - Define the cache manager interface
  3. *
  4. * Copyright (C) 2013 - 2014, Max Lv <max.c.lv@gmail.com>
  5. *
  6. * This file is part of the shadowsocks-libev.
  7. *
  8. * shadowsocks-libev is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * shadowsocks-libev is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with pdnsd; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. */
  22. /*
  23. * Original Author: Oliver Lorenz (ol), olli@olorenz.org, https://olorenz.org
  24. * License: This is licensed under the same terms as uthash itself
  25. */
  26. #ifndef _CACHE_
  27. #define _CACHE_
  28. #include "uthash.h"
  29. #define KEY_MAX_LENGTH 32
  30. /**
  31. * A cache entry
  32. */
  33. struct cache_entry
  34. {
  35. char *key; /**<The key */
  36. void *data; /**<Payload */
  37. UT_hash_handle hh; /**<Hash Handle for uthash */
  38. };
  39. /**
  40. * A cache object
  41. */
  42. struct cache
  43. {
  44. size_t max_entries; /**<Amount of entries this cache object can hold */
  45. struct cache_entry *entries; /**<Head pointer for uthash */
  46. void (*free_cb) (void *element);/**<Callback function to free cache entries */
  47. };
  48. extern int cache_create(struct cache **dst, const size_t capacity,
  49. void (*free_cb) (void *element));
  50. extern int cache_delete(struct cache *cache, int keep_data);
  51. extern int cache_lookup(struct cache *cache, char *key, void *result);
  52. extern int cache_insert(struct cache *cache, char *key, void *data);
  53. extern int cache_remove(struct cache *cache, char *key);
  54. #endif