{"record":{"id":"9a06f1271934d3b2","repo":"ben-manes/caffeine","slug":"key-s-value-was-set-to-s-must-be-a-long","errorCode":null,"errorMessage":"key %s value was set to %s, must be a long","messagePattern":"key (.+?) value was set to (.+?), must be a long","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java","lineNumber":291,"sourceCode":"  static int parseInt(String key, @Nullable String value) {\n    requireArgument((value != null) && !value.isEmpty(), \"value of key %s was omitted\", key);\n    requireNonNull(value);\n    try {\n      return Integer.parseInt(normalizeNumericLiteral(value));\n    } catch (NumberFormatException e) {\n      throw new IllegalArgumentException(String.format(US,\n          \"key %s value was set to %s, must be an integer\", key, value), e);\n    }\n  }\n\n  /** Returns a parsed long value. */\n  static long parseLong(String key, @Nullable String value) {\n    requireArgument((value != null) && !value.isEmpty(), \"value of key %s was omitted\", key);\n    requireNonNull(value);\n    try {\n      return Long.parseLong(normalizeNumericLiteral(value));\n    } catch (NumberFormatException e) {\n      throw new IllegalArgumentException(String.format(US,\n          \"key %s value was set to %s, must be a long\", key, value), e);\n    }\n  }\n\n  /** Returns the value after adjusting for underscores in a numeric literal. */\n  static String normalizeNumericLiteral(String value) {\n    boolean invalid = value.startsWith(\"+_\") || value.startsWith(\"-_\")\n        || value.startsWith(\"_\") || value.endsWith(\"_\");\n    return invalid ? value : value.replace(\"_\", \"\");\n  }\n\n  /** Returns a parsed duration value. */\n  static Duration parseDuration(String key, @Nullable String value) {\n    requireArgument((value != null) && !value.isEmpty(), \"value of key %s omitted\", key);\n    requireNonNull(value);\n\n    boolean isIsoFormat = value.contains(\"p\") || value.contains(\"P\");\n    Duration duration = isIsoFormat","sourceCodeStart":273,"sourceCodeEnd":309,"githubUrl":"https://github.com/ben-manes/caffeine/blob/9da6581ee366aa63c51e0dc96692d02f9c29ccff/caffeine/src/main/java/com/github/benmanes/caffeine/cache/CaffeineSpec.java#L273-L309","documentation":"CaffeineSpec.parseLong is the 64-bit counterpart of parseInt and throws IllegalArgumentException(\"key %s value was set to %s, must be a long\") when a long-valued option (e.g. maximumSize with large values, durations in simple format) cannot be parsed. The key and offending value are included in the message for quick diagnosis.","triggerScenarios":"Caffeine.from(\"maximumSize=9_223_372_036_854_775_808\") (Long.MAX_VALUE+1); a simple duration like \"expireAfterWrite=30x\" reaching parseLong with a non-numeric body; values with commas or scientific notation (\"maximumSize=1e6\").","commonSituations":"Overflowing maximumSize/maximumWeight beyond Long range; locale-formatted or scientific-notation numbers from config systems; malformed duration bodies that fail before the time-unit check.","solutions":["Supply a plain (optionally underscore-separated) decimal integer within the long range","For very large capacities, prefer maximumWeight with an appropriate weighing function instead of raw counts","Validate externally sourced numbers with Long.parseLong before embedding them into a spec string"],"exampleFix":"# before\nCaffeine.from(\"maximumSize=1e9\") // IllegalArgumentException: must be a long\n\n# after\nCaffeine.from(\"maximumSize=1000000000\")","handlingStrategy":"validation","validationCode":"// Validate long options before building the spec:\nString normalized = rawValue.strip().replace(\"_\", \"\").replace(\",\", \"\");\ntry {\n  long v = Long.parseLong(normalized);\n  if (v < 0) throw new ConfigException(\"negative capacity: \" + rawValue);\n} catch (NumberFormatException e) {\n  throw new ConfigException(\"Expected long, got: \" + rawValue, e);\n}","typeGuard":null,"tryCatchPattern":"try {\n  return Caffeine.from(spec);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage() != null && e.getMessage().contains(\"must be a long\")) {\n    throw new ConfigException(\"Invalid long value in cache spec\", e);\n  }\n  throw e;\n}","preventionTips":["Avoid scientific notation and locale-formatted numbers in spec strings","Bound-check capacities against Long.MAX_VALUE before embedding them","Prefer builder APIs (maximumSize(long)) when values are computed programmatically"],"tags":["caffeine","configuration","caffeine-spec","number-parsing","overflow"],"backgroundTag":null,"analyzedSha":"9da6581ee366aa63c51e0dc96692d02f9c29ccff","analyzedAt":"2026-08-14T14:45:28.147Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}