1
// -------------------------------------------------------------------------------- //
2
// Copyright (C) 2008-2010 J.Rios
5
// This Program is free software; you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation; either version 2, or (at your option)
10
// This Program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
// GNU General Public License for more details.
15
// You should have received a copy of the GNU General Public License
16
// along with this program; see the file LICENSE. If not, write to
17
// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18
// http://www.gnu.org/copyleft/gpl.html
20
// -------------------------------------------------------------------------------- //
25
#include <wx/mstream.h>
27
guDbCache * guDbCache::m_DbCache = NULL;
29
// -------------------------------------------------------------------------------- //
30
guDbCache::guDbCache( const wxString &dbname ) : guDb( dbname )
34
query.Add( wxT( "CREATE TABLE IF NOT EXISTS cache( cache_id INTEGER PRIMARY KEY AUTOINCREMENT, "
35
"cache_key varchar, cache_data BLOB, cache_time INTEGER, "
36
"cache_type INTEGER, cache_size INTEGER );" ) );
38
query.Add( wxT( "CREATE UNIQUE INDEX IF NOT EXISTS 'cache_id' on cache( cache_id ASC );" ) );
39
query.Add( wxT( "CREATE INDEX IF NOT EXISTS 'cache_key' on cache( cache_key ASC );" ) );
40
query.Add( wxT( "CREATE INDEX IF NOT EXISTS 'cache_time' on cache( cache_time ASC );" ) );
43
int Count = query.Count();
44
for( Index = 0; Index < Count; Index++ )
46
//guLogMessage( query[ Index ] );
47
ExecuteUpdate( query[ Index ] );
52
// -------------------------------------------------------------------------------- //
53
guDbCache::~guDbCache()
57
// -------------------------------------------------------------------------------- //
58
wxImage * guDbCache::GetImage( const wxString &url, int &imgtype, const int imgsize )
62
wxSQLite3ResultSet dbRes;
63
const unsigned char * Data;
66
query = wxString::Format( wxT( "SELECT cache_data, cache_type FROM cache WHERE cache_key = '%s' "
67
"AND cache_size = %u LIMIT 1;" ),
68
escape_query_str( url ).c_str(), imgsize );
70
dbRes = ExecuteQuery( query );
74
Data = dbRes.GetBlob( 0, DataLen );
75
imgtype = dbRes.GetInt( 1 );
79
wxMemoryInputStream Ins( Data, DataLen );
80
Img = new wxImage( Ins, imgtype );
85
guLogMessage( wxT( "Image is not OK" ) );
92
// guLogMessage( wxT( "Could not create the image" ) );
98
// guLogMessage( wxT( "DbCache failed '%s'" ), url.c_str() );
106
// -------------------------------------------------------------------------------- //
107
bool guDbCache::DoSetImage( const wxString &url, wxImage * img, const int imgtype, const int imagesize )
109
wxMemoryOutputStream Outs;
110
if( img->SaveFile( Outs, imgtype ) )
112
wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT(
113
"INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) "
114
"VALUES( NULL, '%s', ?, %u, %u, %u );" ),
115
escape_query_str( url ).c_str(), imgtype, wxDateTime::Now().GetTicks(), imagesize ) );
117
stmt.Bind( 1, ( const unsigned char * ) Outs.GetOutputStreamBuffer()->GetBufferStart(), Outs.GetSize() );
118
//guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() );
122
catch( wxSQLite3Exception &e )
124
guLogError( wxT( "%u: %s" ), e.GetErrorCode(), e.GetMessage().c_str() );
128
guLogError( wxT( "Other exception found while updating the image in cache" ) );
134
// -------------------------------------------------------------------------------- //
135
bool guDbCache::SetImage( const wxString &url, wxImage * img, const int imgtype )
137
wxImage TmpImg( * img );
140
// int ImageSize = guDBCACHE_IMAGE_SIZE_BIG;
141
// TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
142
guImageResize( &TmpImg, 150 );
143
if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_BIG ) )
150
// ImageSize = guDBCACHE_IMAGE_SIZE_MID;
151
// TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
152
guImageResize( &TmpImg, 100 );
153
if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_MID ) )
159
// ImageSize = guDBCACHE_IMAGE_SIZE_TINY;
160
// TmpImg.Rescale( Width, Height, wxIMAGE_QUALITY_HIGH );
161
guImageResize( &TmpImg, 50 );
162
if( !DoSetImage( url, &TmpImg, imgtype, guDBCACHE_IMAGE_SIZE_TINY ) )
165
// delete the expired entries but call it only 5% of the times
166
if( guRandom( 1000 ) < 20 )
172
// -------------------------------------------------------------------------------- //
173
wxString guDbCache::GetContent( const wxString &url )
175
wxString RetVal = wxEmptyString;
177
wxSQLite3ResultSet dbRes;
179
query = wxString::Format( wxT( "SELECT cache_data FROM cache WHERE cache_key = '%s' LIMIT 1;" ),
180
escape_query_str( url ).c_str() );
182
dbRes = ExecuteQuery( query );
184
if( dbRes.NextRow() )
186
RetVal = dbRes.GetString( 0 );
193
// -------------------------------------------------------------------------------- //
194
bool guDbCache::SetContent( const wxString &url, const char * str, const int len )
197
wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT(
198
"INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) "
199
"VALUES( NULL, '%s', ?, %u, %u, %u );" ),
200
escape_query_str( url ).c_str(), guDBCACHE_TYPE_TEXT, wxDateTime::Now().GetTicks(), 0 ) );
202
stmt.Bind( 1, ( const unsigned char * ) str, len );
203
//guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() );
207
catch( wxSQLite3Exception &e )
209
guLogError( wxT( "%u: %s" ), e.GetErrorCode(), e.GetMessage().c_str() );
214
// -------------------------------------------------------------------------------- //
215
bool guDbCache::SetContent( const wxString &url, const wxString &content )
217
wxString query = wxString::Format( wxT( "INSERT INTO cache( cache_id, cache_key, cache_data, "
218
"cache_type, cache_time, cache_size ) VALUES( NULL, '%s', '%s', %u, %u, %u );" ),
219
escape_query_str( url ).c_str(),
220
escape_query_str( content ).c_str(),
222
wxDateTime::Now().GetTicks(),
225
ExecuteUpdate( query );
227
// delete the expired entries but call it only 2% of the times
228
if( guRandom( 1000 ) < 20 )
234
// -------------------------------------------------------------------------------- //
235
void guDbCache::ClearExpired( void )
237
// last.fm queries are kept only 7 days
238
wxString query = wxString::Format( wxT( "DELETE FROM cache WHERE cache_time < %u AND cache_type = %u" ),
239
wxDateTime::Now().GetTicks() - 604800, guDBCACHE_TYPE_TEXT );
240
ExecuteUpdate( query );
242
// Images are kept 30 days
243
query = wxString::Format( wxT( "DELETE FROM cache WHERE cache_time < %u" ), wxDateTime::Now().GetTicks() - 2592000 );
244
ExecuteUpdate( query );
246
guLogMessage( wxT( "Delete expired Cache elements done" ) );
249
// -------------------------------------------------------------------------------- //