Menu

[r2007]: / framework / trunk / tests / loadable_storage.cpp  Maximize  Restore  History

Download this file

120 lines (106 with data), 3.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#include <cppcms/defs.h>
#include <cppcms/session_storage.h>
#include <cppcms/json.h>
#include <time.h>
#include <map>
namespace {
class test_storage : public cppcms::sessions::session_storage {
public:
///
/// Save session with end of life time at \a timeout using session id \a sid and content \a in
///
virtual void save(std::string const &sid,time_t timeout,std::string const &in)
{
data_[sid]=std::pair<time_t,std::string>(timeout,in);
}
///
/// Load session with \a sid, put its end of life time to \a timeout and return its
/// value to \a out
///
virtual bool load(std::string const &sid,time_t &timeout,std::string &out)
{
data_type::iterator p=data_.find(sid);
if(p==data_.end())
return false;
if(p->second.first < time(0)) {
data_.erase(p);
return false;
}
out=p->second.second;
timeout = p->second.first;
return true;
}
///
/// Remove a session with id \a sid from the storage
///
virtual void remove(std::string const &sid)
{
data_.erase(sid);
}
///
/// Return true of the save or load operations can be blocking
///
virtual bool is_blocking()
{
return false;
}
private:
typedef std::map<std::string,std::pair<time_t,std::string> > data_type;
data_type data_;
};
class test_fact : public cppcms::sessions::session_storage_factory {
public:
///
/// Get a pointer to session_storage. Note if the returned pointer is same for different calls
/// session_storage implementation should be thread safe.
///
virtual booster::shared_ptr<cppcms::sessions::session_storage> get()
{
return storage_;
}
///
/// Return true if session_storage requires garbage collection - removal of expired session time-to-time
///
virtual bool requires_gc() { return false; };
///
/// Delete the object, cleanup
///
virtual ~test_fact() {}
test_fact() :
storage_(new test_storage())
{
}
private:
booster::shared_ptr<cppcms::sessions::session_storage> storage_;
};
} // anon
#if defined(CPPCMS_WIN32)
# define STORAGE_API declspec(__dllexport)
#else
# define STORAGE_API
#endif
extern "C" {
STORAGE_API cppcms::sessions::session_storage_factory *my_sessions_generator(cppcms::json::value const &v)
{
v.get<bool>("must_be_set");
return new test_fact();
}
}
MongoDB Logo MongoDB