-
Notifications
You must be signed in to change notification settings - Fork 16
SQLiteInMemoryDB
Brian Lehnen edited this page Mar 6, 2016
·
2 revisions
You may use in-memory SQLite databases inside of the same process. Here is an example.
To specify an in memory database, use a Uri connection string and set the mode to memory, and the cache to shared.
ConnectionString = "FullUri=file:dbName.db3?mode=memory&cache=shared;Version=3;";
SQLite will release the database if no open connections to the database are present. Because of this, you need to either:
- Keep the creation context alive for as long as the producers and consumers are running.
- Obtain the scope from the creation context and hold onto it while the producers and consumers are running. When processing is complete, dispose of the scope to close the last connection.
The below example uses option #2.
public class SimpleMessage
{
public string Message { get; set; }
}
using System;
using System.Threading.Tasks;
using DotNetWorkQueue;
using DotNetWorkQueue.Logging;
using DotNetWorkQueue.Transport.SQLite.Basic;
using Serilog;
namespace SQLiteMemoryQueue
{
class Program
{
private static readonly string ConnectionString = "FullUri=file:dbName.db3?mode=memory&cache=shared;Version=3;";
private static readonly string QueueName = "testing";
static void Main(string[] args)
{
RunQueue();
}
private static async Task RunQueue()
{
var log = new LoggerConfiguration()
.WriteTo.ColoredConsole(outputTemplate: "{Timestamp:HH:mm} [{Level}] ({Name:l}) {Message}{NewLine}{Exception}")
.CreateLogger();
Log.Logger = log;
using (var scope = CreateQueue())
{
using (var queueContainerSend = new QueueContainer<SqLiteMessageQueueInit>())
{
using (var sendQueue = queueContainerSend.CreateProducer<SimpleMessage>(QueueName, ConnectionString))
{
using (var queueContainerConsumer = new QueueContainer<SqLiteMessageQueueInit>())
{
using (var queue = queueContainerConsumer.CreateConsumer(QueueName, ConnectionString))
{
queue.Start<SimpleMessage>(HandleMessages);
//process user input
var keepRunning = true;
while (keepRunning)
{
Console.WriteLine(@"a) Send 1 jobs
b) Send 10 jobs
c) Send 100 jobs
q) Quit");
var key = char.ToLower(Console.ReadKey(true).KeyChar);
switch (key)
{
case 'a':
await Send(1, sendQueue);
break;
case 'b':
await Send(10, sendQueue);
break;
case 'c':
await Send(100, sendQueue);
break;
case 'q':
Console.WriteLine("Quitting");
keepRunning = false;
break;
}
}
}
}
}
}
}
}
private static async Task Send(int messageCount, IProducerQueue<SimpleMessage> queue)
{
for (var i = 0; i < messageCount; i++)
{
var result = await queue.SendAsync(new SimpleMessage {Message = "Hello World"});
if (result.SendingException != null)
{
Console.WriteLine(result.SendingException);
}
}
}
private static void HandleMessages(IReceivedMessage<SimpleMessage> message, IWorkerNotification notifications)
{
notifications.Log.Log(LogLevel.Info, () => $"Processing message {message.MessageId.Id.Value}");
}
private static ICreationScope CreateQueue()
{
using (var createQueueContainer = new QueueCreationContainer<SqLiteMessageQueueInit>())
{
using (var createQueue = createQueueContainer.GetQueueCreation<SqLiteMessageQueueCreation>(QueueName, ConnectionString))
{
createQueue.CreateQueue();
return createQueue.Scope; //hold onto the scope to prevent the queue from being deleted
}
}
}
}
}
For any issues please use the GitHub issues