<commit_before>defmodule Survey.Cache do
  use Survey.Web, :model
  alias Survey.Repo

  schema "cache" do
    field :blob, Survey.Term
  end

  def store(blob) do
    {:ok, id} = Repo.transaction(fn ->
      case Repo.get_by(Survey.Cache, blob: blob) do
        %{id: id} -> id
        nil -> insert(blob)
      end
    end)
    id
  end

  defp insert(blob) do
    %{id: id} = %Survey.Cache{blob: blob}  |> Repo.insert!
    id
  end

  def delete(id) do
    Survey.Repo.delete_all(from f in Survey.Cache, where: f.id == ^id)
  end

  def get(id) do
    case Repo.get(Survey.Cache, id) do
      %{blob: blob} -> blob
      nil           -> nil
    end
  end
end<commit_msg>Making cache more robust<commit_after>defmodule Survey.Cache do
  use Survey.Web, :model
  alias Survey.Repo
  import Ecto.Query
  require Ecto.Query
  alias Survey.Cache

  schema "cache" do
    field :blob, Survey.Term
  end

  def store(blob) do
    {:ok, id} = Repo.transaction(fn ->

      case from(f in Cache, where: f.blob == ^blob, limit: 1) do
        %{id: id} -> id
        nil -> insert(blob)
      end
    end)
    id
  end

  defp insert(blob) do
    %{id: id} = %Survey.Cache{blob: blob}  |> Repo.insert!
    id
  end

  def delete(id) do
    Survey.Repo.delete_all(from f in Survey.Cache, where: f.id == ^id)
  end

  def get(id) do
    case Repo.get(Survey.Cache, id) do
      %{blob: blob} -> blob
      nil           -> nil
    end
  end
end
{'fraction_non_alphanumeric': 0.12334494773519164, 'fraction_numerical': 0.0006968641114982578, 'mean_word_length': 2.156043956043956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '469188', 'n_tokens_mistral': 542, 'n_tokens_neox': 532, 'n_words': 155}