Browse Source

- Backported unordered from 1.46.1

- Switched from using of std::map to use of boost::unordered_map
  for primary key index
master
Artyom Beilis 13 years ago
parent
commit
7907de457d
6 changed files with 51 additions and 17 deletions
  1. BIN
      cppcms_boost.tar.bz2
  2. +1
    -1
      private/cache_storage.h
  3. +27
    -6
      private/shmem_allocator.h
  4. +4
    -3
      src/cache_pool.cpp
  5. +18
    -6
      src/cache_storage.cpp
  6. +1
    -1
      tests/cache_backend_test.cpp

BIN
cppcms_boost.tar.bz2 View File


+ 1
- 1
private/cache_storage.h View File

@@ -26,7 +26,7 @@ namespace cppcms {
namespace impl {
booster::intrusive_ptr<base_cache> CPPCMS_API thread_cache_factory(unsigned items);
#ifndef CPPCMS_WIN32
booster::intrusive_ptr<base_cache> CPPCMS_API process_cache_factory(size_t memory);
booster::intrusive_ptr<base_cache> CPPCMS_API process_cache_factory(size_t memory,unsigned items);
#endif
} // impl
} // cppcms


+ 27
- 6
private/shmem_allocator.h View File

@@ -92,10 +92,11 @@ public :


template<typename U> shmem_allocator (const shmem_allocator< U, mm > &) { };

shmem_allocator (const shmem_allocator &){ };
shmem_allocator() {};

inline pointer allocate(size_type cnt, std::allocator<void>::const_pointer = 0) const
pointer allocate(size_type cnt, std::allocator<void>::const_pointer = 0) const
{
void *memory=mm->malloc(cnt*sizeof(T));
if(!memory) {
@@ -103,15 +104,35 @@ public :
}
return (pointer)memory;
};
inline void deallocate(pointer p, size_type) const
void deallocate(pointer p, size_type) const
{
mm->free(p);
};
inline void construct(pointer p, const T& t) const { new(p) T(t); }
inline void destroy(pointer p) const { p->~T(); }
void construct(pointer p, const T& t) const
{
new(p) T(t);
}
void destroy(pointer p) const
{
p->~T();
}
pointer address(reference x) const
{
return &x;
}
const_pointer address(const_reference x) const
{
return &x;
}

inline bool operator==(shmem_allocator const&) const { return true; }
inline bool operator!=(shmem_allocator const& a) const { return false; }
bool operator==(shmem_allocator const&) const
{
return true;
}
bool operator!=(shmem_allocator const& a) const
{
return false;
}
};




+ 4
- 3
src/cache_pool.cpp View File

@@ -52,10 +52,11 @@ cache_pool::cache_pool(json::value const &settings) :
#elif defined(CPPCMS_NO_PREFOK_CACHE)
throw cppcms_error("The 'process_shared' backend is disabled during build procedure");
#else // has prefork cache
size_t memory = settings.get("cache.memory",16384) * 1024;
if(memory < 65536)
size_t memory = settings.get("cache.memory",16384);
if(memory < 64)
throw cppcms_error("'process_shared' cache backend requires at least 64 KB of cache memory: cache.memory >= 64");
d->module=impl::process_cache_factory(memory);
unsigned items = settings.get("cache.limit",memory);
d->module=impl::process_cache_factory(memory*1024,items);
#endif // prefork cache
}
else


+ 18
- 6
src/cache_storage.cpp View File

@@ -22,6 +22,8 @@
#include "cache_storage.h"
#include <booster/thread.h>



#if defined CPPCMS_WIN32
# ifndef CPPCMS_NO_PREFOK_CACHE
# define CPPCMS_NO_PREFOK_CACHE
@@ -36,10 +38,13 @@
#include <map>
#include <list>
#include <limits>
#include <iostream>
#include <time.h>
#include <cppcms/cstdint.h>


#include <cppcms_boost/unordered/unordered_map.hpp>
namespace boost = cppcms_boost;

namespace cppcms {
namespace impl {

@@ -143,10 +148,11 @@ class mem_cache : public base_cache {

typedef std::basic_string<char,std::char_traits<char>,allocator > string_type;

typedef std::map<
typedef boost::unordered_map<
string_type,
container,
std::less<string_type>,
boost::hash<string_type>,
std::equal_to<string_type>,
typename allocator::template rebind<std::pair<const string_type,container> >::other
> map_type;

@@ -227,11 +233,16 @@ public:
refs(0),
generation(0)
{
nl_clear();
}
~mem_cache()
{
}
void set_size(unsigned l) { limit=l; };
void set_size(unsigned l)
{
limit = l;
nl_clear();
};
virtual bool fetch(std::string const &key,std::string *a,std::set<std::string> *triggers,time_t *timeout_out,uint64_t *gen)
{
rdlock_guard lock(*access_lock);
@@ -290,6 +301,7 @@ public:
lru.clear();
primary.clear();
triggers.clear();
primary.rehash( (limit+1) / primary.max_load_factor() + 1);
size = 0;
}
virtual void clear()
@@ -412,10 +424,10 @@ booster::intrusive_ptr<base_cache> thread_cache_factory(unsigned items)
}

#if !defined(CPPCMS_NO_PREFOK_CACHE)
booster::intrusive_ptr<base_cache> process_cache_factory(size_t memory)
booster::intrusive_ptr<base_cache> process_cache_factory(size_t memory,unsigned items)
{
process_settings::init(memory);
return new mem_cache<process_settings>(0);
return new mem_cache<process_settings>(items);
}
#endif



+ 1
- 1
tests/cache_backend_test.cpp View File

@@ -130,7 +130,7 @@ int main()
std::cout << "Ok" << std::endl;
#if !defined(CPPCMS_WIN32) && !defined(CPPCMS_NO_PREFOK_CACHE)
std::cout << "Testing process cache... " << std::flush;
test_cache(cppcms::impl::process_cache_factory(16*1024));
test_cache(cppcms::impl::process_cache_factory(16*1024,20));
std::cout << "Ok" << std::endl;
#endif


Loading…
Cancel
Save