Пример #1
0
        public SqliteCache(SqliteCacheOptions options, ILogger <SqliteCache>?logger = null)
        {
            _config = options;
            _logger = logger ?? new NullLogger <SqliteCache>();

            // Silence warnings about variables not initialized in constructor because they ARE
            // initialized in the call to `Connect()`.
            _db      = null !;
            Commands = null !;
            Connect();

            // Directly checking _db/Commands will cause Roslyn to think they may be null
            var x = _db;

            Debug.Assert(x != null);
            var y = Commands;

            Debug.Assert(y != null);

            // This has to be after the call to Connect()
            if (_config.CleanupInterval.HasValue)
            {
                _cleanupTimer = new Timer(_ =>
                {
                    _logger.LogTrace("Beginning background cache cleanup");
                    RemoveExpired();
                    _logger.LogTrace("Completed background cache cleanup");
                }, null, TimeSpan.Zero, _config.CleanupInterval.Value);
            }
        }
Пример #2
0
        private void Connect()
        {
            if (_db == null)
            {
                var connectionString = _config.ConnectionString;
                _logger.LogTrace("Opening connection to SQLite database: " +
                                 "{ConnectionString}", connectionString);

                // First try to open an existing database
                if (!_config.MemoryOnly && System.IO.File.Exists(_config.CachePath))
                {
                    _logger.LogTrace("Found existing database at {CachePath}", _config.CachePath);

                    var db = new DbConnection(_config.ConnectionString);
                    db.Open();
                    if (CheckExistingDb(db))
                    {
                        // Everything checks out, we can use this as our cache db
                        _db = db;
                    }
                    else
                    {
                        if (db is not null)
                        {
                            _logger.LogTrace("Closing connection to SQLite database at {SqliteCacheDbPath}", _config.CachePath);
                            db.Close();
                            db.Dispose();
                        }

                        _logger.LogInformation("Deleting existing incompatible cache db file {CachePath}", _config.CachePath);
                        System.IO.File.Delete(_config.CachePath);
                    }
                }

                if (_db == null)
                {
                    _db = new DbConnection(_config.ConnectionString);
                    _db.Open();
                    Initialize();
                }

                Commands = new DbCommandPool(_db, _logger);

                // Explicitly set default journal mode and fsync behavior
                using (var cmd = new DbCommand("PRAGMA journal_mode = WAL;", _db))
                {
                    cmd.ExecuteNonQuery();
                }
                using (var cmd = new DbCommand("PRAGMA synchronous = NORMAL;", _db))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
Пример #3
0
        public async ValueTask ConnectAsync(CancellationToken cancel)
        {
            if (_db == null)
            {
                var connectionString = _config.ConnectionString;
                _logger.LogTrace("Opening connection to SQLite database: " +
                                 "{ConnectionString}", connectionString);

                // First try to open an existing database
                if (!_config.MemoryOnly && System.IO.File.Exists(_config.CachePath))
                {
                    _logger.LogTrace("Found existing database at {CachePath}", _config.CachePath);

                    var db = new SqliteConnection(_config.ConnectionString);
                    await db.OpenAsync();

                    if (await CheckExistingDbAsync(db, cancel))
                    {
                        // Everything checks out, we can use this as our cache db
                        _db = db;
                    }
                    else
                    {
                        db?.Dispose();
                        db?.Close();

                        _logger.LogInformation("Deleting existing incompatible cache db file {CachePath}", _config.CachePath);
                        System.IO.File.Delete(_config.CachePath);
                    }
                }

                if (_db == null)
                {
                    _db = new DbConnection(_config.ConnectionString);
                    await _db.OpenAsync();
                    await InitializeAsync(cancel);
                }

                Commands = new DbCommandPool(_db, _logger);
            }
        }
Пример #4
0
        private void Connect()
        {
            if (_db == null)
            {
                var connectionString = _config.ConnectionString;
                _logger.LogTrace("Opening connection to SQLite database: " +
                                 "{ConnectionString}", connectionString);

                // First try to open an existing database
                if (!_config.MemoryOnly && System.IO.File.Exists(_config.CachePath))
                {
                    _logger.LogTrace("Found existing database at {CachePath}", _config.CachePath);

                    var db = new DbConnection(_config.ConnectionString);
                    db.Open();
                    if (CheckExistingDb(db))
                    {
                        // Everything checks out, we can use this as our cache db
                        _db = db;
                    }
                    else
                    {
                        db?.Dispose();
                        db?.Close();

                        _logger.LogInformation("Deleting existing incompatible cache db file {CachePath}", _config.CachePath);
                        System.IO.File.Delete(_config.CachePath);
                    }
                }

                if (_db == null)
                {
                    _db = new DbConnection(_config.ConnectionString);
                    _db.Open();
                    Initialize();
                }

                Commands = new DbCommandPool(_db, _logger);
            }
        }