<commit_before>require "shopify_unlimited/version"
require 'shopify_unlimited/active_resource/base_extensions'
require 'shopify_unlimited/active_resource/connection_extensions'

module ShopifyUnlimited
  SHOPIFY_CREDIT_LIMIT_PERIOD = 5.minutes

  class << self
    attr_accessor :use_memcached
    def memcached
      return nil unless use_memcached
      @memcached ||= Dalli::Client.new()
    end

    def cache_key
      URI.parse(ShopifyAPI::Base.site.to_s).host + "_api_reset_time"
    end

    def cached_time(max)
      now = Time.now
      result = now
      unless memcached.add(cache_key, now, 500)
        memcached.cas(cache_key, 500) do |cached_time|
          result = ((now - cached_time) > max) ? now : cached_time
          result
        end 
      end
      result
    end

    def set_cached_time(time)

      memcached.set(cache_key, time, 500)
    end  


    def estimated_time_until_reset
      credit_record = ShopifyAPI::Base.connection.shopify_credit
      time = credit_record.time unless credit_record.nil?
      time ||= cached_time(ShopifyUnlimited::SHOPIFY_CREDIT_LIMIT_PERIOD)
      est = ShopifyUnlimited::SHOPIFY_CREDIT_LIMIT_PERIOD - (Time.now - time)
      [est, 0].max
    end<commit_msg>fix error if use_memcached false<commit_after>require "shopify_unlimited/version"
require 'shopify_unlimited/active_resource/base_extensions'
require 'shopify_unlimited/active_resource/connection_extensions'

module ShopifyUnlimited
  SHOPIFY_CREDIT_LIMIT_PERIOD = 5.minutes

  class << self
    attr_accessor :use_memcached
    def memcached
      return nil unless use_memcached
      @memcached ||= Dalli::Client.new()
    end

    def cache_key
      URI.parse(ShopifyAPI::Base.site.to_s).host + "_api_reset_time"
    end

    def cached_time(max)
      now = Time.now
      result = now
      return result unless memcached
      unless memcached.add(cache_key, now, 500)
        memcached.cas(cache_key, 500) do |cached_time|
          result = ((now - cached_time) > max) ? now : cached_time
          result
        end 
      end
      result
    end

    def set_cached_time(time)
      return unless memcached
      memcached.set(cache_key, time, 500)
    end  


    def estimated_time_until_reset
      credit_record = ShopifyAPI::Base.connection.shopify_credit
      time = credit_record.time unless credit_record.nil?
      time ||= cached_time(ShopifyUnlimited::SHOPIFY_CREDIT_LIMIT_PERIOD)
      est = ShopifyUnlimited::SHOPIFY_CREDIT_LIMIT_PERIOD - (Time.now - time)
      [est, 0].max
    end
{'fraction_non_alphanumeric': 0.0780449349625542, 'fraction_numerical': 0.008671659440283799, 'mean_word_length': 3.1402936378466557, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '7186995', 'n_tokens_mistral': 854, 'n_tokens_neox': 810, 'n_words': 162}