Class: OpenAssets::Cache::TransactionCache

Inherits:
SQLiteBase
  • Object
show all
Defined in:
lib/openassets/cache/transaction_cache.rb

Overview

An object that can be used for caching serialized transaction in a Sqlite database.

Instance Attribute Summary

Attributes inherited from SQLiteBase

#db

Instance Method Summary collapse

Methods inherited from SQLiteBase

#initialize

Constructor Details

This class inherits a constructor from OpenAssets::Cache::SQLiteBase

Instance Method Details

#get(txid) ⇒ Object

Return the serialized transaction. @param txid The transaction id. @return The serialized transaction. If not found transaction, return nil.



19
20
21
22
# File 'lib/openassets/cache/transaction_cache.rb', line 19

def get(txid)
  rows = db.execute('SELECT SerializedTx FROM Tx WHERE TransactionHash = ?', [txid])
  rows.empty? ? nil : rows[0][0]
end

#put(txid, serialized_tx) ⇒ Object

Saves a serialized transaction in cache. @param txid A transaction id. @param serialized_tx A a hex-encoded serialized transaction.



27
28
29
# File 'lib/openassets/cache/transaction_cache.rb', line 27

def put(txid, serialized_tx)
  db.execute('INSERT INTO Tx (TransactionHash, SerializedTx) VALUES (?, ?)', [txid, serialized_tx])
end

#setupObject



7
8
9
10
11
12
13
14
# File 'lib/openassets/cache/transaction_cache.rb', line 7

def setup
  db.execute <<-SQL
    CREATE TABLE IF NOT EXISTS Tx(
            TransactionHash BLOB,
            SerializedTx BLOB,
            PRIMARY KEY (TransactionHash))
  SQL
end